一、Xpath
XPath 是一门在 XML 和 HTML 文档中查找信息的语言,用于在 XML 和 HTML 文档中通过元素和属性进行导航。
谷歌浏览器安装 Xpath Heleper
百度一下谷歌浏览器安装 Xpath Heleper,对着操作即可
启动快捷键: shift + ctrl + x
火狐浏览器安装 Try Xpath
百度一下谷歌浏览器安装 Try Xpath,对着操作即可
1、XPath 节点
在 XPath 中,有七种类型的节点:元素、属性、文本、命名空间、处理指令、注释以及文档(根)节点。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>
解释:
1、为文档节点(根节点)
2、J K. Rowling 为元素节点
3、lang=“en” 为属性节点
节点关系:
1、book 元素是 title、author、year 以及 price 元素的父
2、title、author、year 以及 price 元素都是 book 元素的子
3、title、author、year 以及 price 元素都是同胞
4、title 元素的先辈是 book 元素和 bookstore 元素
5、bookstore 的后代是 book、title、author、year 以及 price 元素
图解:
2、XPath 语法
1、基本语法
表达式 | 描述 |
---|---|
nodename | 选取此节点的所有子节点。 |
/ | 从根节点选取。 |
// | 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。 |
. | 选取当前节点。 |
… | 选取当前节点的父节点。 |
@ | 选取属性。 |
2、使用方法
<?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>
解释:
路径表达式 | 结果 |
---|---|
bookstore | 选取 bookstore 元素的所有子节点。 |
/bookstore | 选取根元素 bookstore。注释:假如路径起始于正斜杠( / ),则此路径始终代表到某元素的绝对路径! |
bookstore/book | 选取属于 bookstore 的子元素的所有 book 元素。 |
//book | 选取所有 book 子元素,而不管它们在文档中的位置。 |
bookstore//book | 选择属于 bookstore 元素的后代的所有 book 元素,而不管它们位于 bookstore 之下的什么位置。 |
//@lang | 选取名为 lang 的所有属性。 |
3、谓语选取
路径表达式 | 结果 |
---|---|
/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。 |
4、选取未知节点
路径表达式 | 结果 |
---|---|
/bookstore/* | 选取 bookstore 元素的所有子元素。 |
//* | 选取文档中的所有元素。 |
//title[@*] | 选取所有带有属性的 title 元素。 |
5、选取若干路径
路径表达式 | 结果 |
---|---|
//book/title 丨 //book/price | 选取 book 元素的所有 title 和 price 元素。 |
//title丨//price | 选取文档中的所有 title 和 price 元素。 |
/bookstore/book/title丨 //price | 选取属于 bookstore 元素的 book 元素的所有 title 元素,以及文档中所有的 price 元素。 |
3、XPath 轴
轴名称 | 结果 |
---|---|
ancestor | 选取当前节点的所有先辈(父、祖父等)。 |
ancestor-or-self | 选取当前节点的所有先辈(父、祖父等)以及当前节点本身。 |
attribute | 选取当前节点的所有属性。 |
child | 选取当前节点的所有子元素。 |
descendant | 选取当前节点的所有后代元素(子、孙等)。 |
descendant-or-self | 选取当前节点的所有后代元素(子、孙等)以及当前节点本身。 |
following | 选取文档中当前节点的结束标签之后的所有节点。 |
following-sibling | 选取当前节点之后的所有兄弟节点 |
namespace | 选取当前节点的所有命名空间节点。 |
parent | 选取当前节点的父节点。 |
preceding | 选取文档中当前节点的开始标签之前的所有节点。 |
preceding-sibling | 选取当前节点之前的所有同级节点。 |
self | 选取当前节点。 |
4、XPath 运算符
运算符 | 描述 | 实例 | 返回值 |
---|---|---|---|
丨 | 计算两个节点集 | //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 |
二、lxml 库的使用
lxml 是一个 HTML/XML 的解析器,主要功能是如何解析和提取 HTML/XML 的数据。
1、安装lxml库
1、dos窗口输入 pip install lxml 进行安装
2、pycharm 中的 setting 中搜索 lxml 库进行安装
2、基本使用
2.1、从变量中读取 html 代码
from lxml import etree
text = '''
<?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>
'''
html = etree.HTML(text)
print(etree.tostring(html).decode('utf-8'))
解释:
1、from lxml import etree : 从lxml中导入etree模块
2、etree.HTML(text):将text中的html代码通过此方法读取,返回一个html对象
3、etree.tostring(html).decode(‘utf-8’):把html对象先读取成字节码,然后通过decode解码成utf-8类型。
2.2、从文件中读取html代码
from lxml import etree
html = etree.parse('demo.html')
result = etree.tostring(html)
print(result.decode('utf-8'))
3、通过 lxml 使用 Xpath
from lxml import etree
html = etree.parse('demo.html')
# 获取所有li标签
res1 = html.xpath('//li')
for i in res1:
print(etree.tostring(i).decode('utf-8'))
# 获取所有li元素下的class属性值
res2 = html.xpath('//li/@class')
print(res2)
# 获取li标签下href为 www.baidu.com的a标签
res3 = html.xpath('//li/a[@href="www.baidu.com"]')
for i in res3:
print(etree.tostring(i).decode('utf-8'))
# 获取li标签下的所有span标签
res4 = html.xpath('//li//span')
for i in res4:
print(etree.tostring(i).decode('utf-8'))
# 获取li标签下的a标签的class属性值
res5 = html.xpath('//li/a//@class')
print(res5)
# 获取最后一个li标签下的a标签的href属性对应值
res6 = html.xpath('//li[last()]/a/@href')
print(res6)
# 获取倒数第二个li元素的内容(1)
res7 = html.xpath('//li[last()-1]/a')
print(res7[0].text)
# 获取倒数第二个li元素的内容(2)
res8 = html.xpath('//li[last()-1]/a/text()')
print(res8)