Xpath 介绍
Xpath(xml path language)
XPath 使用路径表达式在 XML 文档中进行导航 XPath 包含一个标准函数库 XPath 是 XSLT 中的主要元素 XPath 是一个 W3C 标准
节点
在 XPath 中, 有七种类型的节点: 元素属性文本命名空间处理指令注释以及文档 (根) 节点 XML 文档是被作为节点树来对待的树的根被称为文档节点或者根节点
请看下面这个 XML 文档:
- <?xml version="1.0" encoding="UTF-8"?>
- <bookstore>
- <book>
- <title lang="en">Harry Potter</title>
- <author>J K. Rowling</author>
- <year>2005</year>
- <price>29.99</price>
- </book>
- </bookstore>
上面的 XML 文档中的节点例子:
<bookstore> (文档节点)
<author>J K. Rowling</author> (元素节点)
lang="en" (属性节点)
基本值
基本值 (或称原子值, Atomic value) 是无父或无子的节点
基本值的例子:
J K. Rowling "en"
项目
项目 (Item) 是基本值或者节点
节点关系
父(Parent)
每个元素以及属性都有一个父
在下面的例子中, book 元素是 titleauthoryear 以及 price 元素的父:
- <book>
- <title>Harry Potter</title>
- <author>J K. Rowling</author>
- <year>2005</year>
- <price>29.99</price>
- </book>
子(Children)
元素节点可有零个一个或多个子
在下面的例子中, titleauthoryear 以及 price 元素都是 book 元素的子:
- <book>
- <title>Harry Potter</title>
- <author>J K. Rowling</author>
- <year>2005</year>
- <price>29.99</price>
- </book>
同胞(Sibling)
拥有相同的父的节点
在下面的例子中, titleauthoryear 以及 price 元素都是同胞:
- <book>
- <title>Harry Potter</title>
- <author>J K. Rowling</author>
- <year>2005</year>
- <price>29.99</price>
- </book>
先辈(Ancestor)
某节点的父父的父, 等等
在下面的例子中, title 元素的先辈是 book 元素和 bookstore 元素:
- <bookstore>
- <book>
- <title>Harry Potter</title>
- <author>J K. Rowling</author>
- <year>2005</year>
- <price>29.99</price>
- </book>
- </bookstore>
后代(Descendant)
某个节点的子, 子的子, 等等
在下面的例子中, bookstore 的后代是 booktitleauthoryear 以及 price 元素:
- <bookstore>
- <book>
- <title>Harry Potter</title>
- <author>J K. Rowling</author>
- <year>2005</year>
- <price>29.99</price>
- </book>
- </bookstore>
XPath 语法
XPath 使用路径表达式来选取 XML 文档中的节点或节点集节点是通过沿着路径 (path) 或者步 (steps) 来选取的
XML 实例文档
- <?xml version="1.0" encoding="UTF-8"?>
- <bookstore>
- <book>
- <title lang="eng">Harry Potter</title>
- <price>29.99</price>
- </book>
- <book>
- <title lang="eng">Learning XML</title>
- <price>39.95</price>
- </book>
- </bookstore>
选取节点
XPath 使用路径表达式在 XML 文档中选取节点节点是通过沿着路径或者 step 来选取的 下面列出了最有用的路径表达式:
表达式 | 描述 |
---|---|
nodename | 选取此节点的所有子节点。 |
/ | 从根节点选取。 |
// | 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。 |
. | 选取当前节点。 |
.. | 选取当前节点的父节点。 |
@ | 选取属性。 |
在下面的表格中, 我们已列出了一些路径表达式以及表达式的结果:
路径表达式 | 结果 |
---|---|
bookstore | 选取 bookstore 元素的所有子节点。 |
/bookstore | 选取根元素 bookstore。注释:假如路径起始于正斜杠 ( / ),则此路径始终代表到某元素的绝对路径! |
bookstore/book | 选取属于 bookstore 的子元素的所有 book 元素。 |
//book | 选取所有 book 子元素,而不管它们在文档中的位置。 |
bookstore//book | 选择属于 bookstore 元素的后代的所有 book 元素,而不管它们位于 bookstore 之下的什么位置。 |
//@lang | 选取名为 lang 的所有属性。 |
谓语(Predicates)
谓语用来查找某个特定的节点或者包含某个指定的值的节点
谓语被嵌在方括号中
在下面的表格中, 我们列出了带有谓语的一些路径表达式, 以及表达式的结果:
路径表达式 | 结果 |
---|---|
/bookstore/book[1] | 选取属于 bookstore 子元素的第一个 book 元素。 |
/bookstore/book[last()] | 选取属于 bookstore 子元素的最后一个 book 元素。 |
/bookstore/book[last()-1] | 选取属于 bookstore 子元素的倒数第二个 book 元素。 |
/bookstore/book[position()<3] | 选取最前面的两个属于 bookstore 元素的子元素的 book 元素。 |
//title[@lang] | 选取所有拥有名为 lang 的属性的 title 元素。 |
//title[@lang=‘eng‘] | 选取所有 title 元素,且这些元素拥有值为 eng 的 lang 属性。 |
/bookstore/book[price>35.00] | 选取 bookstore 元素的所有 book 元素,且其中的 price 元素的值须大于 35.00。 |
/bookstore/book[price>35.00]/title | 选取 bookstore 元素中的 book 元素的所有 title 元素,且其中的 price 元素的值须大于 35.00。 |
选取未知节点
XPath 通配符可用来选取未知的 XML 元素
通配符 | 描述 |
---|---|
* | 匹配任何元素节点。 |
@* | 匹配任何属性节点。 |
node() | 匹配任何类型的节点。 |
在下面的表格中, 我们列出了一些路径表达式, 以及这些表达式的结果:
路径表达式 | 结果 |
---|---|
/bookstore/* | 选取 bookstore 元素的所有子元素。 |
//* | 选取文档中的所有元素。 |
//title[@*] | 选取所有带有属性的 title 元素。 |
选取若干路径
通过在路径表达式中使用 "|" 运算符, 您可以选取若干个路径
在下面的表格中, 我们列出了一些路径表达式, 以及这些表达式的结果:
路径表达式 | 结果 |
---|---|
//book/title | //book/price | 选取 book 元素的所有 title 和 price 元素。 |
//title | //price | 选取文档中的所有 title 和 price 元素。 |
/bookstore/book/title | //price | 选取属于 bookstore 元素的 book 元素的所有 title 元素,以及文档中所有的 price 元素。 |
XPath 轴(Axes)
轴可定义相对于当前节点的节点集
轴名称 | 结果 |
---|---|
ancestor | 选取当前节点的所有先辈(父、祖父等)。 |
ancestor-or-self | 选取当前节点的所有先辈(父、祖父等)以及当前节点本身。 |
attribute | 选取当前节点的所有属性。 |
child | 选取当前节点的所有子元素。 |
descendant | 选取当前节点的所有后代元素(子、孙等)。 |
descendant-or-self | 选取当前节点的所有后代元素(子、孙等)以及当前节点本身。 |
following | 选取文档中当前节点的结束标签之后的所有节点。 |
following-sibling | 选取当前节点之后的所有兄弟节点 |
namespace | 选取当前节点的所有命名空间节点。 |
parent | 选取当前节点的父节点。 |
preceding | 选取文档中当前节点的开始标签之前的所有节点。 |
preceding-sibling | 选取当前节点之前的所有同级节点。 |
self | 选取当前节点。 |
XPath 运算符
下面列出了可用在 XPath 表达式中的运算符:
运算符 | 描述 | 实例 | 返回值 |
---|---|---|---|
| | 计算两个节点集 | //book | //cd | 返回所有拥有 book 和 cd 元素的节点集 |
+ | 加法 | 6 + 4 | 10 |
- | 减法 | 6 - 4 | 2 |
* | 乘法 | 6 * 4 | 24 |
div | 除法 | 8 div 4 | 2 |
= | 等于 | price=9.80 | 如果 price 是 9.80,则返回 true。如果 price 是 9.90,则返回 false。 |
!= | 不等于 | price!=9.80 | 如果 price 是 9.90,则返回 true。如果 price 是 9.80,则返回 false。 |
< | 小于 | price<9.80 | 如果 price 是 9.00,则返回 true。如果 price 是 9.90,则返回 false。 |
<= | 小于或等于 | price<=9.80 | 如果 price 是 9.00,则返回 true。如果 price 是 9.90,则返回 false。 |
> | 大于 | price>9.80 | 如果 price 是 9.90,则返回 true。如果 price 是 9.80,则返回 false。 |
>= | 大于或等于 | price>=9.80 | 如果 price 是 9.90,则返回 true。如果 price 是 9.70,则返回 false。 |
or | 或 | price=9.80 or price=9.70 | 如果 price 是 9.80,则返回 true。如果 price 是 9.50,则返回 false。 |
and | 与 | price>9.00 and price<9.90 | 如果 price 是 9.80,则返回 true。如果 price 是 8.50,则返回 false。 |
mod | 计算除法的余数 | 5 mod 2 | 1 |
获取网页元素
还有一种方法使用 chrome.
Python 中使用 XPath
Python 中可以使用 lxml 库加 XPath 来解析 Xhtml 文档, 用法如下
- from lxml import etree
- Selector = etree.HTML(XHTML)
Selector.xpath(xpath 语言)
基于以上的 xml 文档进行如下的示例
选取所有的书名
- # -*-coding:utf8-*-
- from lxml import etree
- html = '''<?xml version="1.0"encoding="UTF-8"?>
- <bookstore>
- <book>
- <title lang="eng">Harry Potter</title>
- <price>29.99</price>
- </book>
- <book>
- <title lang="eng">Learning XML</title>
- <price>39.95</price>
- </book>
- </bookstore>
- '''
- selector = etree.HTML(html)
- # 提取文本
- content = selector.xpath('//book/title/text()')
- for each in content:
- print(each)
运行结果:
- Harry Potter
- Learning XML
- [Finished in 0.2s]
查看有多少种语言的书
- # -*-coding:utf8-*-
- from lxml import etree
- html = '''<?xml version="1.0"encoding="UTF-8"?>
- <bookstore>
- <book>
- <title lang="eng">Harry Potter</title>
- <price>29.99</price>
- </book>
- <book>
- <title lang="eng">Learning XML</title>
- <price>39.95</price>
- </book>
- </bookstore>
- '''
- selector = etree.HTML(html)
- # 提取属性
- link = set(selector.xpath('//title/@lang'))
- for each in link:
- print(each)
运行结果:
- eng
- [Finished in 0.2s]
查找拥有属性 lang 的 title 节点
- # -*-coding:utf8-*-
- from lxml import etree
- html = '''<?xml version="1.0"encoding="UTF-8"?>
- <bookstore>
- <book>
- <title lang="eng">Harry Potter</title>
- <price>29.99</price>
- </book>
- <book>
- <title lang="eng">Learning XML</title>
- <price>39.95</price>
- </book>
- </bookstore>
- '''
- selector = etree.HTML(html)
- # # 提取文本
- # content = selector.xpath('//book/title/text()')
- # for each in content:
- # print(each)
- # # 提取属性
- # link = set(selector.xpath('//title/@lang'))
- # for each in link:
- # print(each)
- title = selector.xpath('//title[@lang]')
- print(title)
运行结果:
- [<Element title at 0x4d4ef48>, <Element title at 0x4d4ef08>]
- [Finished in 0.2s]
提取属性以 xxx 开头的节点
- #-*-coding:utf8-*-
- from lxml import etree
- html1 = '''
- <!DOCTYPE html>
- <html>
- <head lang="en">
- <meta charset="UTF-8">
- <title></title>
- </head>
- <body>
- <div id="test-1">需要的内容 1</div>
- <div id="test-2">需要的内容 2</div>
- <div id="testfault">需要的内容 3</div>
- </body>
- </html>
- '''
- selector = etree.HTML(html1)
- content = selector.xpath('//div[starts-with(@id,"test")]/text()')
- for each in content:
- print(each)
运行结果:
需要的内容 1
需要的内容 2
需要的内容 3
[Finished in 0.4s]
提取嵌套标签中的所有文本
- #-*-coding:utf8-*-
- from lxml import etree
- html2 = '''
- <!DOCTYPE html>
- <html>
- <head lang="en">
- <meta charset="UTF-8">
- <title></title>
- </head>
- <body>
- <div id="test3">
我左青龙,
<span id="tiger">
右白虎,
<ul > 上朱雀,
- <li > 下玄武</li>
- </ul>
老牛在当中,
</span>
龙头在胸口
- </div>
- </body>
- </html>
- '''
- selector = etree.HTML(html2)
- data = selector.xpath('//div[@id="test3"]')[0]
- info = data.xpath('string(.)')
- content_2 = info.replace('\n', '').replace(' ','')
- print(content_2)
运行结果:
我左青龙, 右白虎, 上朱雀, 下玄武老牛在当中, 龙头在胸口
[Finished in 0.3s]
来源: http://www.bubuko.com/infodetail-2506930.html