Xpath的全称是XML Path Language,即XML路径语言,用来在XML文档中查找信息。它虽然最初是用来搜寻XML文档的,但同样适用于HTML文档的搜索。
| 表达式 | 描述 |
| nodename | 选取此节点的所有节点 |
| / | 从当前节点选取直接子节点 |
| // | 从当前节点选取子孙节点 |
| . | 选取当前节点 |
| .. | 选取当前节点的父节点 |
| @ | 选取属性 |
- from lxml import etree
-
- text = '''
- '''
- # 调用HTML类进行初始化,构造一个XPath解析对象
- # etree模块可以自动修复HTML代码
- html = etree.HTML(text)
- # 输出修正后的HTML代码,但结果是bytes类型
- result = etree.tostring(html)
- # 利用decode方法将其转换为str类型
- print(result.decode('utf-8'))
- from lxml import etree
-
- text = '''
- '''
- # 调用HTML类进行初始化,构造一个XPath解析对象
- # etree模块可以自动修复HTML代码
- html = etree.HTML(text)
- # # 输出修正后的HTML代码,但结果是bytes类型
- # result = etree.tostring(html)
- # # 利用decode方法将其转换为str类型
- # print(result.decode('utf-8'))
- # *表示匹配所有节点
- result = html.xpath('//*')
- print(result)
- from lxml import etree
-
- text = '''
- '''
- # 调用HTML类进行初始化,构造一个XPath解析对象
- # etree模块可以自动修复HTML代码
- html = etree.HTML(text)
- # # 输出修正后的HTML代码,但结果是bytes类型
- # result = etree.tostring(html)
- # # 利用decode方法将其转换为str类型
- # print(result.decode('utf-8'))
- # *表示匹配所有节点
- # result = html.xpath('//*')
- # print(result)
-
- # 子节点
- result = html.xpath('//li/a')
- print(result)
- from lxml import etree
-
- text = '''
- '''
- # 调用HTML类进行初始化,构造一个XPath解析对象
- # etree模块可以自动修复HTML代码
- html = etree.HTML(text)
- # # 输出修正后的HTML代码,但结果是bytes类型
- # result = etree.tostring(html)
- # # 利用decode方法将其转换为str类型
- # print(result.decode('utf-8'))
- # *表示匹配所有节点
- # result = html.xpath('//*')
- # print(result)
-
- # 子节点
- # result = html.xpath('//li/a')
- # print(result)
-
- # 父节点
- result = html.xpath('//a[@href="link4.html"]/../@class')
- print(result)
- from lxml import etree
-
- text = '''
- '''
- # 调用HTML类进行初始化,构造一个XPath解析对象
- # etree模块可以自动修复HTML代码
- html = etree.HTML(text)
- # # 输出修正后的HTML代码,但结果是bytes类型
- # result = etree.tostring(html)
- # # 利用decode方法将其转换为str类型
- # print(result.decode('utf-8'))
- # *表示匹配所有节点
- # result = html.xpath('//*')
- # print(result)
-
- # 子节点
- # result = html.xpath('//li/a')
- # print(result)
-
- # 父节点
- # result = html.xpath('//a[@href="link4.html"]/../@class')
- # print(result)
-
- # 属性匹配
- result = html.xpath('//li[@class="item-0"]')
- print(result)
- from lxml import etree
-
- text = '''
- '''
- # 调用HTML类进行初始化,构造一个XPath解析对象
- # etree模块可以自动修复HTML代码
- html = etree.HTML(text)
- # # 输出修正后的HTML代码,但结果是bytes类型
- # result = etree.tostring(html)
- # # 利用decode方法将其转换为str类型
- # print(result.decode('utf-8'))
- # *表示匹配所有节点
- # result = html.xpath('//*')
- # print(result)
-
- # 子节点
- # result = html.xpath('//li/a')
- # print(result)
-
- # 父节点
- # result = html.xpath('//a[@href="link4.html"]/../@class')
- # print(result)
-
- # 属性匹配
- # result = html.xpath('//li[@class="item-0"]')
- # print(result)
-
- # 文本获取
- result = html.xpath('//li[@class="item-0"]/a/text()')
- print(result)
- from lxml import etree
-
- text = '''
- '''
- # 调用HTML类进行初始化,构造一个XPath解析对象
- # etree模块可以自动修复HTML代码
- html = etree.HTML(text)
- # # 输出修正后的HTML代码,但结果是bytes类型
- # result = etree.tostring(html)
- # # 利用decode方法将其转换为str类型
- # print(result.decode('utf-8'))
- # *表示匹配所有节点
- # result = html.xpath('//*')
- # print(result)
-
- # 子节点
- # result = html.xpath('//li/a')
- # print(result)
-
- # 父节点
- # result = html.xpath('//a[@href="link4.html"]/../@class')
- # print(result)
-
- # 属性匹配
- # result = html.xpath('//li[@class="item-0"]')
- # print(result)
-
- # 文本获取
- # result = html.xpath('//li[@class="item-0"]/a/text()')
- # print(result)
-
- # 属性获取
- result = html.xpath('//li/a/@href')
- print(result)
- text2 = '''
- '''
- html2 = etree.HTML(text2)
- result = html2.xpath('//li[contains(@class, "li")]/a/text()')
- print(result)
- # 多属性匹配
- text2 = '''
- '''
- html2 = etree.HTML(text2)
- result = html2.xpath('//li[contains(@class, "li") and @name="item"]/a/text()')
- print(result)
- # 按序选择
- # 注意:序号从1开始,不是从0开始
- result = html.xpath('//li[1]/a/text()')
- print(result)
- result = html.xpath('//li[last()]/a/text()')
- print(result)
- result = html.xpath('//li[position()>3]/a/text()')
- print(result)
- result = html.xpath('//li[last()-2]/a/text()')
- print(result)
- # 节点轴选择
- # 调用ancestor轴,获取所有祖先节点。这里使用*,表示匹配所有节点。
- result = html.xpath('//li[1]/ancestor::*')
- print(result)
- # 添加限制条件,只得到div节点的祖先节点
- result = html.xpath('//li[1]/ancestor::div')
- print(result)
- # 调用attribute轴,获得所有属性值
- result = html.xpath('//li[1]/attribute::*')
- print(result)
- # 调用child轴,可以获得所有子节点。这里加入限定条件,选取href="link1.html"的a节点
- result = html.xpath('//li[1]/child::a[@href="link1.html"]')
- print(result)
- # 调用descendant轴,获取所有子孙节点。这里加入限定条件,获取span节点
- result = html.xpath('//li[1]/descendant::span')
- print(result)
- # 调用following轴,可以获得当前节点之后的所有节点。这里设置了索引选择,,只获得第二个后继节点
- result = html.xpath('//li[1]/following::*[2]')
- print(result)
- # 调用following—sibling轴,可以获得当前节点之后的所有同级节点
- result = html.xpath('//li[1]/following-sibling::*')
- print(result)