Python模块之 lxml 是一个 xpath 格式解析模块

作用:

  是一个 xpath 格式解析模块

必要操作:

安装:

1
2
3
pip install lxml
easy_install lxml

 导入包:

1
from lxml import etree

帮助查看:

1
help(etree)

方法(函数):

1.解析离线网页:

1 html=etree.parse('xx.html',etree.HTMLParser())
2 aa=html.xpath('//*[@id="s_xmancard_news"]/div/div[2]/div/div[1]/h2/a[1]/@href')
3 print(aa)

 

2.解析在线网页:

1 from lxml import etree
2 import requests
3 rep=requests.get('https://www.baidu.com')
4 html=etree.HTML(rep.text)
5 aa=html.xpath('//*[@id="s_xmancard_news"]/div/div[2]/div/div[1]/h2/a[1]/@href')
6 print(aa)

 

 

 解析规则:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#使用text构造一个XPath解析对象,etree模块可以自动修正HTML文本
html = lxml.etree.HTML(text)
 
#直接读取文本进行解析
html = lxml.etree.parse('./ex.html',etree.HTMLParser())
 
#选取所有节点
result = html.xpath('//*')
 
#获取所有li节点
result = html.xpath('//li')
 
#获取所有li节点的直接a子节点
result = html.xpath('//li/a')
 
#获取所有li节点的所有a子孙节点
result = html.xpath('//li//a')
 
#获取所有href属性为link.html的a节点的父节点的class属性
result = html.xpath('//a[@href="link.html"]/../@class')
 
#获取所有class属性为ni的li节点
result = html.xpath('//li[@class="ni"]')
 
#获取所有li节点的文本
result = html.xpath('//li/text()')
 
#获取所有li节点的a节点的href属性
result = html.xpath('//li/a/@href')
 
#当li的class属性有多个值时,需用contains函数完成匹配
result = html.xpath('//li[contains(@class,"li")]/a/text())
 
#多属性匹配
result = html.xpath('//li[contains(@class,"li") and @name="item"]/a/text()')
result = html.xpath('//li[1]/a/text()')
result = html.xpath('//li[last()]/a/text()')
result = html.xpath('//li[position()<3]/a/text()')
result = html.xpath('//li[last()-2]/a/text()')
 
#按序选择,中括号内为XPath提供的函数
result = html.xpath('//li[1]/ancestor::*')
#获取祖先节点
result = html.xpath('//li[1]/ancestor::div')
 
#获取属性值
result = html.xpath('//li[1]/attribute::*')
 
#获取直接子节点
result = html.xpath('//li[1]/child::a[@href="link1.html"]')
 
#获取所有子孙节点
result = html.xpath('//li[1]/descendant::span')
 
#获取当前节点之后的所有节点的第二个
result = html.xpath('//li[1]/following::*[2]')
 
#获取后续所有同级节点
result = html.xpath('//li[1]/following-sibling::*')

 

 

 

 

参考:

https://zhuanlan.zhihu.com/p/356791704

posted @   悟透  阅读(54)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
点击右上角即可分享
微信分享提示