python3 xpath_Python3 【解析库XPath】

一.XPath简介

对网页的层级关系进行解析,XPath的选择功能十分强大,它提供了非常简洁明了的路径选择表达式。

另外,它还提供了超过100个内建函数,用于字符串、数值、时间的匹配以及节点、序列的处理等,

几乎所有的定位节点,都可以用XPath进行选择。

官网: https://www.w3.org/TR/xpath

1.XPath常用规则:

1653024-20200418135641454-702638061.png

二.基本的使用

from lxml importetree

text= '''

1

2

3

4

5

'''

#将文本转换为网页类型,并修复补全

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 importetree

text= '''

1

2

3

4

5

'''

#将文本转换为网页类型,并修复补全

html =etree.HTML(text)#选择内容匹配

result = html.xpath('//*')print(result)

1653024-20200418142507496-1507315080.png

2.子节点

from lxml importetree

text= '''

1

2

3

4

5

'''

#将文本转换为网页类型,并修复补全

html =etree.HTML(text)#选择内容匹配

result = html.xpath('//li/a')print(result)

这里"/"代表的是直接的子节点,"//"代表是所有的子孙节点

1653024-20200418142637433-1961480100.png

3.父节点

父节点:使用"..",也可以使用parent::代表父级

from lxml importetree

text= '''

1

2

3

4

5

'''

#将文本转换为网页类型,并修复补全

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)

1653024-20200418143740681-1298385186.png

4.文本获取

from lxml importetree

text= '''

1

2

3

4

5

'''

#将文本转换为网页类型,并修复补全

html =etree.HTML(text)#选择内容匹配#属性为link4的a标签的父级的class属性

result = html.xpath('//a[@href="link4"]/text()')print(result)

1653024-20200418144217880-2022890005.png

5.属性多值匹配

from lxml importetree

text= '''

1

2

3

4

5

'''

#将文本转换为网页类型,并修复补全

html =etree.HTML(text)#选择内容匹配#contains(@属性,值)

result = html.xpath('//li[contains(@class,"three")]/a/text()')print(result)

6.多属性匹配

多个属性确定一个节点,这时就需要匹配多个属性

from lxml importetree

text= '''

1

2

3

4

5

'''

#将文本转换为网页类型,并修复补全

html =etree.HTML(text)#选择内容匹配#contains(@属性,值)

result = html.xpath('//li[contains(@class,"three") and @name="item"]/a/text()')print(result)

7.按序选择

from lxml importetree

text= '''

1

2

3

4

5

'''

#将文本转换为网页类型,并修复补全

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)

1653024-20200418145809582-2133926837.png

8.节点轴选择

1.ancestor

from lxml importetree

text= '''

1

2

3

4

5

'''

#将文本转换为网页类型,并修复补全

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.

ContractedBlock.gif

ExpandedBlockStart.gif

from lxml importetree

text= '''

1

2

3

4

5

'''

#将文本转换为网页类型,并修复补全

html =etree.HTML(text)#选择内容匹配

result= html.xpath('//li[1]/ancestor::div')#内置函数100,http://www.w3school.com.cn/xpath/xpath_functions.asp

print(result)

View Code

我们又加了限制条件,这次在冒号后面加了div,这样得到的结果就只有div这个祖先节点了。

2.atrribute

ContractedBlock.gif

ExpandedBlockStart.gif

from lxml importetree

text= '''

1

2

3

4

5

'''

#将文本转换为网页类型,并修复补全

html =etree.HTML(text)#选择内容匹配

result= html.xpath('//li[1]/attribute::*')#内置函数100,http://www.w3school.com.cn/xpath/xpath_functions.asp

print(result)

View Code

我们调用了attribute轴,可以获取所有属性值,其后跟的选择器还是*,这代表获取节点的所有属性,返回值就是li节点的所有属性值

3.child

我们调用了child轴,可以获取所有直接子节点。这里我们又加了限定条件,选取href

属性为link1的a节点

ContractedBlock.gif

ExpandedBlockStart.gif

from lxml importetree

text= '''

1

2

3

4

5

'''

#将文本转换为网页类型,并修复补全

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)

View Code

4.descendant

我们调用了descendant轴,可以获取所有子孙节点。这里我们又加了限制条件获取

span节点,所以返回的结果只包含span节点而不包含a节点

ContractedBlock.gif

ExpandedBlockStart.gif

from lxml importetree

text= '''

1

2

3

4

5

'''

#将文本转换为网页类型,并修复补全

html =etree.HTML(text)#选择内容匹配

result= html.xpath('//li[1]/descendant::span')#内置函数100,http://www.w3school.com.cn/xpath/xpath_functions.asp

print(result)

View Code

5.following

我们调用了following轴,可以获取当前节点之后的所有节点。这里我们虽然使用的是

*匹配,但又加了索引选择,所以只获取了第二个后续节点。

ContractedBlock.gif

ExpandedBlockStart.gif

from lxml importetree

text= '''

1

2

3

4

5

'''

#将文本转换为网页类型,并修复补全

html =etree.HTML(text)#选择内容匹配

result= html.xpath('//li[1]/following::*[2]')#内置函数100,http://www.w3school.com.cn/xpath/xpath_functions.asp

print(result)

View Code

6.following-siblings

ContractedBlock.gif

ExpandedBlockStart.gif

from lxml importetree

text= '''

1

2

3

4

5

'''

#将文本转换为网页类型,并修复补全

html =etree.HTML(text)#选择内容匹配

result= html.xpath('//li[1]/following-sibling::*')#内置函数100,http://www.w3school.com.cn/xpath/xpath_functions.asp

print(result)

View Code

我们调用了following-sibling轴,可以获取当前节点之后的所有同级节点。这里我们

使用*匹配,所以获取了所有后续同级节点。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值