xpath作用
- 在XML文件中查找信息的一套规则/语言,根据XML的元素或者属性进行遍历。
- 也适用于html文档
Xpath 开发工具
- 开源表达式编辑工具:XMLQuire
- Chorme插件:Xpath Helper
- 可以使用谷歌浏览器直接粘贴xpath路径,但是可能通用性不强
使用方法
- 安装lxml(二选一)
<code>pip install lxml
conda install lxml/<code>
- 导入etree
<code>from lxml import etree/<code>
- 构建etree树
<code>from lxml import etree
text = """
<title>这是标题/<title>
这是段落1
这是段落2
这是段落2
"""
html = etree.HTML(text)/<code>
选取节点
- nodename:选取此节点的所有节点
- /:根节点或者下一节点
<code>result = html.xpath('/html')
print(result)
"""
[<element>]
"""/<element>/<code>
- // :选取节点,不考虑位置
<code>`//` :选取节点,不考虑位置/<code>
- . :选取当前节点
<code>result = html.xpath('//div')
for r in result:
s = r.xpath('./p') # 选取当前div节点下的p节点
print(s)
"""
[<element>]
[<element>]
[<element>]
"""/<element>/<element>/<element>/<code>
- .. :选取当前节点的父节点
<code>result = html.xpath('//div') # 选取div节点
for r in result:
s = r.xpath('..') # 选取当前节点的父节点,div的父节点是body
print(s)
"""
[<element>]
[<element>]
[<element>]
"""/<element>/<element>/<element>/<code>
- @:选取属性
<code>result = html.xpath('//div[@style="color:#FF0000"]') # 选取div节点,其中style = "color:#FF0000"
print(result)
"""
[<element>]
"""/<element>/<code>
- 注意:
- / :一般安装路径查找,表示它的子节点
- // :表示它的后代,包括子、孙
提取属性或者文本
- text():提取当前节点的文本
<code>result = html.xpath('//div/p/text()') # 选取//div/p下的文本
print(result)
"""
['这是段落1', '这是段落2', '这是段落2']
"""/<code>
- string(.):提取当前节点以及子孙节点的所有文本
<code>result = html.xpath('string(.)')
print(result)
"""
这是标题
这是段落1
这是段落2
这是段落2
"""/<code>
- @:提取某个属性,以提取百度官网的所有url为例
<code>mport requests
from lxml import etree
from pprint import pprint
import re
url = 'https://www.baidu.com'
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)\\
Chrome/80.0.3987.149 Safari/537.36'
}
response = requests.get(url, headers=headers)
text = response.text
html = etree.HTML(text)
result = html.xpath('//a/@href') # 选取所有a节点下的链接
p = re.compile('http.?://.*?') # 编写正则表达式,提取http或者https开头的网页
list2 = []
for r in result:
result2 = p.match(r) # 检测是否匹配
if result2: # 如果匹配
list2.append(r)
pprint(list2)
"""
['https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F&sms=5',
'https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_pc_1',
'http://news.baidu.com',
'https://www.hao123.com',
'http://map.baidu.com',
'http://v.baidu.com',
'http://tieba.baidu.com',
'http://xueshu.baidu.com',
'https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F&sms=5',
'http://www.baidu.com/gaoji/preferences.html',
'http://www.baidu.com/more/',
'http://ir.baidu.com',
'http://e.baidu.com/?refer=888',
'http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=11000002000001',
'http://tieba.baidu.com/f?kw=&fr=wwwt',
'http://zhidao.baidu.com/q?ct=17&pn=0&tn=ikaslist&rn=10&word=&fr=wwwt',
'http://music.taihe.com/search?fr=ps&ie=utf-8&key=',
'http://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=',
'http://v.baidu.com/v?ct=301989888&rn=20&pn=0&db=0&s=25&ie=utf-8&word=',
'http://map.baidu.com/m?word=&fr=ps01000',
'http://wenku.baidu.com/search?word=&lm=0&od=0&ie=utf-8']
"""/<code>
谓语-Predicates
- 未完待续...
- 如果文字看不太懂,可以关注我,然后点击头像,可以查看配套视频。
閱讀更多 潮辦公 的文章