Python3 【解析库XPath】
一.XPath简介
对网页的层级关系进行解析,XPath的选择功能十分强大,它提供了非常简洁明了的路径选择表达式。
另外,它还提供了超过100个内建函数,用于字符串、数值、时间的匹配以及节点、序列的处理等,
几乎所有的定位节点,都可以用XPath进行选择。
官网: https://www.w3.org/TR/xpath
1.XPath常用规则:
二.基本的使用
from lxml import etree text = ''' <div> <ul> <li class="one"><a href="link1">1</a></li> <li class="two"><a href="link2">2</a></li> <li class="three"><a href="link3">3</a></li> <li class="four"><a href="link4">4</a></li> <li class="five"><a href="link5">5</a> </ul> </div> ''' #将文本转换为网页类型,并修复补全 html = etree.HTML(text) #将网页整体补为网页结构,打开文件路径 #html = etree.parse('demo.html',etree.HTMLParser()) print(html) #将网页转换为文本类型,为bytes result = etree.tostring(html) #转化为str类型 result = result.decode("utf-8") print(result)
1.匹配选择(所有节点)
from lxml import etree text = ''' <div> <ul> <li class="one"><a href="link1">1</a></li> <li class="two"><a href="link2">2</a></li> <li class="three"><a href="link3">3</a></li> <li class="four"><a href="link4">4</a></li> <li class="five"><a href="link5">5</a> </ul> </div> ''' #将文本转换为网页类型,并修复补全 html = etree.HTML(text) #选择内容匹配 result = html.xpath('//*') print(result)
2.子节点
from lxml import etree text = ''' <div> <ul> <li class="one"><a href="link1">1</a></li> <li class="two"><a href="link2">2</a></li> <li class="three"><a href="link3">3</a></li> <li class="four"><a href="link4">4</a></li> <li class="five"><a href="link5">5</a> </ul> </div> ''' #将文本转换为网页类型,并修复补全 html = etree.HTML(text) #选择内容匹配 result = html.xpath('//li/a') print(result)
这里"/"代表的是直接的子节点,"//"代表是所有的子孙节点
3.父节点
父节点:使用"..",也可以使用parent::代表父级
from lxml import etree text = ''' <div> <ul> <li class="one"><a href="link1">1</a></li> <li class="two"><a href="link2">2</a></li> <li class="three"><a href="link3">3</a></li> <li class="four"><a href="link4">4</a></li> <li class="five"><a href="link5">5</a> </ul> </div> ''' #将文本转换为网页类型,并修复补全 html = etree.HTML(text) #选择内容匹配 #属性为link4的a标签的父级的class属性 result = html.xpath('//a[@href="link4"]/../@class') #@表示属性 result1 = html.xpath('//a[@href="link4"]/parent::*/@class') print(result) print(result1)
4.文本获取
from lxml import etree text = ''' <div> <ul> <li class="one"><a href="link1">1</a></li> <li class="two"><a href="link2">2</a></li> <li class="three"><a href="link3">3</a></li> <li class="four"><a href="link4">4</a></li> <li class="five"><a href="link5">5</a> </ul> </div> ''' #将文本转换为网页类型,并修复补全 html = etree.HTML(text) #选择内容匹配 #属性为link4的a标签的父级的class属性 result = html.xpath('//a[@href="link4"]/text()') print(result)
5.属性多值匹配
from lxml import etree text = ''' <div> <ul> <li class="one"><a href="link1">1</a></li> <li class="two"><a href="link2">2</a></li> <li class="three two"><a href="link3">3</a></li> <li class="four"><a href="link4">4</a></li> <li class="five"><a href="link5">5</a> </ul> </div> ''' #将文本转换为网页类型,并修复补全 html = etree.HTML(text) #选择内容匹配 #contains(@属性,值) result = html.xpath('//li[contains(@class,"three")]/a/text()') print(result)
6.多属性匹配
多个属性确定一个节点,这时就需要匹配多个属性
from lxml import etree text = ''' <div> <ul> <li class="one"><a href="link1">1</a></li> <li class="two three" name="item"><a href="link2">2</a></li> <li class="three two"><a href="link3">3</a></li> <li class="four"><a href="link4">4</a></li> <li class="five"><a href="link5">5</a> </ul> </div> ''' #将文本转换为网页类型,并修复补全 html = etree.HTML(text) #选择内容匹配 #contains(@属性,值) result = html.xpath('//li[contains(@class,"three") and @name="item"]/a/text()') print(result)
7.按序选择
from lxml import etree text = ''' <div> <ul> <li class="one"><a href="link1">1</a></li> <li class="two three" name="item"><a href="link2">2</a></li> <li class="three two"><a href="link3">3</a></li> <li class="four"><a href="link4">4</a></li> <li class="five"><a href="link5">5</a> </ul> </div> ''' #将文本转换为网页类型,并修复补全 html = etree.HTML(text) #选择内容匹配 #匹配第一个li result1 = html.xpath('//li[1]/a/text()') #最后一个倒数2 result2 = html.xpath('//li[last()-2]/a/text()') #最后一个 result3 = html.xpath('//li[last()]/a/text()') #小于3 result4 = html.xpath('//li[position()<3]/a/text()') #内置函数100,http://www.w3school.com.cn/xpath/xpath_functions.asp print(result1) print(result2) print(result3) print(result4)
8.节点轴选择
1.ancestor
from lxml import etree text = ''' <div> <ul> <li class="one"><a href="link1">1</a></li> <li class="two three" name="item"><a href="link2">2</a></li> <li class="three two"><a href="link3">3</a></li> <li class="four"><a href="link4">4</a></li> <li class="five"><a href="link5">5</a> </ul> </div> ''' #将文本转换为网页类型,并修复补全 html = etree.HTML(text) #选择内容匹配 result = html.xpath('//li[1]/ancestor::*') #内置函数100,http://www.w3school.com.cn/xpath/xpath_functions.asp print(result)
我们调用了ancestor轴,可以获取所有祖先节点。其后需要跟两个冒号,然后是节点的选择器,这里直接使用*,表示匹配所有的节点,因此返回结果是第一个li节点的所有祖先节点,包括html,body,div和ul.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from lxml import etree text = ''' <div> <ul> <li class="one"><a href="link1">1</a></li> <li class="two three" name="item"><a href="link2">2</a></li> <li class="three two"><a href="link3">3</a></li> <li class="four"><a href="link4">4</a></li> <li class="five"><a href="link5">5</a> </ul> </div> ''' #将文本转换为网页类型,并修复补全 html = etree.HTML(text) #选择内容匹配 result = html.xpath('//li[1]/ancestor::div') #内置函数100,http://www.w3school.com.cn/xpath/xpath_functions.asp print(result)
我们又加了限制条件,这次在冒号后面加了div,这样得到的结果就只有div这个祖先节点了。
2.atrribute
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from lxml import etree text = ''' <div> <ul> <li class="one"><a href="link1">1</a></li> <li class="two three" name="item"><a href="link2">2</a></li> <li class="three two"><a href="link3">3</a></li> <li class="four"><a href="link4">4</a></li> <li class="five"><a href="link5">5</a> </ul> </div> ''' #将文本转换为网页类型,并修复补全 html = etree.HTML(text) #选择内容匹配 result = html.xpath('//li[1]/attribute::*') #内置函数100,http://www.w3school.com.cn/xpath/xpath_functions.asp print(result)
我们调用了attribute轴,可以获取所有属性值,其后跟的选择器还是*,这代表获取节点的所有属性,返回值就是li节点的所有属性值
3.child
我们调用了child轴,可以获取所有直接子节点。这里我们又加了限定条件,选取href
属性为link1的a节点
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from lxml import etree text = ''' <div> <ul> <li class="one"><a href="link1">1</a></li> <li class="two three" name="item"><a href="link2">2</a></li> <li class="three two"><a href="link3">3</a></li> <li class="four"><a href="link4">4</a></li> <li class="five"><a href="link5">5</a> </ul> </div> ''' #将文本转换为网页类型,并修复补全 html = etree.HTML(text) #选择内容匹配 result = html.xpath('//li[1]/child::a[@href="link1"]') #内置函数100,http://www.w3school.com.cn/xpath/xpath_functions.asp print(result)
4.descendant
我们调用了descendant轴,可以获取所有子孙节点。这里我们又加了限制条件获取
span节点,所以返回的结果只包含span节点而不包含a节点
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from lxml import etree text = ''' <div> <ul> <li class="one"><a href="link1"><span>1</span></a></li> <li class="two three" name="item"><a href="link2">2</a></li> <li class="three two"><a href="link3">3</a></li> <li class="four"><a href="link4">4</a></li> <li class="five"><a href="link5">5</a> </ul> </div> ''' #将文本转换为网页类型,并修复补全 html = etree.HTML(text) #选择内容匹配 result = html.xpath('//li[1]/descendant::span') #内置函数100,http://www.w3school.com.cn/xpath/xpath_functions.asp print(result)
5.following
我们调用了following轴,可以获取当前节点之后的所有节点。这里我们虽然使用的是
*匹配,但又加了索引选择,所以只获取了第二个后续节点。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from lxml import etree text = ''' <div> <ul> <li class="one"><a href="link1"><span>1</span></a></li> <li class="two three" name="item"><a href="link2">2</a></li> <li class="three two"><a href="link3">3</a></li> <li class="four"><a href="link4">4</a></li> <li class="five"><a href="link5">5</a> </ul> </div> ''' #将文本转换为网页类型,并修复补全 html = etree.HTML(text) #选择内容匹配 result = html.xpath('//li[1]/following::*[2]') #内置函数100,http://www.w3school.com.cn/xpath/xpath_functions.asp print(result)
6.following-siblings
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from lxml import etree text = ''' <div> <ul> <li class="one"><a href="link1"><span>1</span></a></li> <li class="two three" name="item"><a href="link2">2</a></li> <li class="three two"><a href="link3">3</a></li> <li class="four"><a href="link4">4</a></li> <li class="five"><a href="link5">5</a> </ul> </div> ''' #将文本转换为网页类型,并修复补全 html = etree.HTML(text) #选择内容匹配 result = html.xpath('//li[1]/following-sibling::*') #内置函数100,http://www.w3school.com.cn/xpath/xpath_functions.asp print(result)
我们调用了following-sibling轴,可以获取当前节点之后的所有同级节点。这里我们
使用*匹配,所以获取了所有后续同级节点。