使用 XPath 选择器

在前面的内容中,我们掌握了一些 CSS 选择器和它们的使用方法,以及 rvest 包中
用于提取网页内容的函数。
一般来说,CSS 选择器足够满足绝大部分
的 HTML 节点匹配的需要。但是,当需要根据某些
特殊条件选择节点时,需要用更强大的技术。
图 14-5 所示的网页比 data/products.html 复杂
一点:
这个网页作为一个独立的 HTML 文件被存储
在 data/new-products.html。全部的源代码很长,这
里只展示 <body> 部分。请浏览一遍源代码,以便
对它的结构有个印象:
<body>
<h1>New Products</h1>

 


图 14-5
<p>The following is a list of products</p>
<div id = "list" class = "product-list">
<ul>
<li>
<span class = "name">Product-A</span>
<span class = "price">$199.95</span>
<div class = "info bordered">
<p>Description for Product-A</p>
<ul>
<li><span class = "info-key">Quality</span> <span class =
"infovalue">Good</span></li>
<li><span class = "info-key">Duration</span> <span class =
"infovalue">5 </span><span class = "unit">years</span></li>
</ul>
</div>
</li>
<li class = "selected">
<span class = "name">Product-B</span>
<span class = "price">$129.95</span>
<div class = "info">
<p>Description for Product-B</p>
<ul>
<li><span class = "info-key">Quality</span> <span class = "infovalue">
Medium</span></li>
<li><span class = "info-key">Duration</span> <span class = "infovalue">
2</span><span class = "unit">years</span></li>
</ul>
</div>
</li>
<li>
<span class = "name">Product-C</span>
<span class = "price">$99.95</span>
<div class = "info">
<p>Description for Product-C</p>
<ul>
<li><span class = "info-key">Quality</span> <span class = "infovalue">
Good</span></li>
<li><span class = "info-key">Duration</span> <span class = "infovalue">
4</span><span class = "unit">years</span></li>
</ul>
</div>
</li>
</ul>
</div>
<p>All products are available for sale!</p>
</body>
网页的源代码包含了一个样式表和产品详细信息的列表。每个产品都有其描述和很多
性质。接下来,就像前面的例子一样,我们载入网页:
page <- read_ _html("data/new-products.html")
HTML 的代码结构简单明晰。在深入挖掘 XPath 之前,我们需要了解一下 XML。编写
良好且组织规范的 HTML 文档可以被看作 XML(eXtensive Markup Language)文档的一个
特例。与 HTML 不同,XML 允许任意的标签和属性。下面是一个简单的 XML 文档示例:
<?xml version = "1.0"?>
<root>
<product id = "1">
<name>Product-A<name>
<price>$199.95</price>
</product>
<product id = "2">
<name>Product-B</name>
<price>$129.95</price>
</product>
</root>
XPath 专门用于提取 XML 文档中的数据。在本节中,我们比较 XPath 表达式和 CSS 选
择器,查看二者在提取网页数据过程中的作用。
函数 html_node( ) 和 html_nodes( ) 支持 XPath 表达式,并通过参数 xpath= 实
现。表 14-2 展示了 CSS 选择器和等价的 XPath 表达式之间的一些重要对比。
表 14-2
CSS XPath Math
li > * //li/* All children of <li>
li[attr] //li[@attr] All <li> with attr attribute
li[attr=value] //li[@attr = 'value'] <li attr = "value">
li#item //li[@id = 'item'] <li id = "item">
li.info //li[contains(@class,'info')] <li class = "info">
续表
CSS XPath Math
li:first-child //li[1] First <li>
li:last-child //li[last()] Last <li>
li:nth-child(n) //li[n] n th <li>
(N/A) //p[a] All <p> with a child <a>
(N/A) //p[position() <= 5] The first five <p> nodes
(N/A) //p[last()-2] The last third last <p>
(N/A) //li[value>0.5] All <li> with child <value>whose value > 0.5
CSS 选择器会匹配所有子层级的节点。在 XPath 表达式中,标签 // 和 / 匹配不同的
节点。更具体地说,// 标签引用所有子层级的 <tag> 节点,而 / 标签只引用第 1 个子层级
的 <tag> 节点。
我们通过下面这些例子展示它们的用法:
选择所有 <p> 节点:
page %>% html_ _nodes(xpath = "//p")
## {xml_nodeset (5)}
## [1] <p>The following is a list of products</p>
## [2] <p>Description for Product-A</p>
## [3] <p>Description for Product-B</p>
## [4] <p>Description for Product-C</p>
## [5] <p>All products are available for sale!</p>
选择所有具有 class 属性的 <li> 节点:
page %>% html_ _nodes(xpath = "//li[@class]")
## {xml_nodeset (1)}
## [1] <li class = "selected">\n <span class = "name">Pro ...
选择 <div id = "list"><ul> 节点中所有 <li> 子节点:
page %>% html_ _nodes(xpath = "//div[@id = 'list']/ul/li")
## {xml_nodeset (3)}
## [1] <li>\n <span class = "name">Product-A</span>\n ...
## [2] <li class = "selected">\n <span class = "name">Pro ...
## [3] <li>\n <span class = "name">Product-C</span>\n ...
选择所有嵌套于<div id = "list"> 中 <li> 标签下的 <span class = "name"> 子
节点:
page %>% html_ _nodes(xpath = "//div[@id = 'list']//li/span[@class = 'name']")
## {xml_nodeset (3)}
## [1] <span class = "name">Product-A</span>
## [2] <span class = "name">Product-B</span>
## [3] <span class = "name">Product-C</span>
选择所有嵌套于 <li class = "selected"> 中的 <span class = "name"> 子节点:
page %>%
html_ _nodes(xpath = "//li[@class = 'selected']/span[@class = 'name']")
## {xml_nodeset (1)}
## [1] <span class = "name">Product-B</span>
上面这些例子也可以使用等效的 CSS 选择器来实现。然而,下面这些例子就不能
用 CSS 选择器实现了:
选择所有包含 <p> 子节点的 <div> 节点:
page %>% html_ _nodes(xpath = "//div[p]")
## {xml_nodeset (3)}
## [1] <div class = "info bordered">\n <p>Description ...
## [2] <div class = "info">\n <p>Description for Prod ...
## [3] <div class = "info">\n <p>Description for Prod ...
选择所有的 <span class = "info-value">Good</span>:
page %>%
html_ _nodes(xpath = "//span[@class = 'info-value' and text() = 'Good']")
## {xml_nodeset (2)}
## [1] <span class = "info-value">Good</span>
## [2] <span class = "info-value">Good</span>
选择所有优质产品的名称:
page %>%
html_ _nodes(xpath = "//li[div/ul/li[1]/span[@class = 'info-value' and
text() = 'Good']]/span[@class = 'name']")
## {xml_nodeset (2)}
## [1] <span class = "name">Product-A</span>
## [2] <span class = "name">Product-C</span>
选择所有持续时间超过 3 年的产品名称:
page %>%
html_ _nodes(xpath = "//li[div/ul/li[2]/span[@class = 'info-value' and
text()>3]]/span[@class = 'name']")
## {xml_nodeset (2)}
## [1] <span class = "name">Product-A</span>
## [2] <span class = "name">Product-C</span>
XPath 是非常灵活的,在匹配网页节点方面是一个强大的工具。想要了解更多内容,
请访问 http://www.w3schools.com/xsl/xpath_syntax.aspac。

posted @ 2019-02-11 14:47  NAVYSUMMER  阅读(196)  评论(0编辑  收藏  举报
交流群 编程书籍