当前位置:网站首页>【爬虫】数据提取之xpath
【爬虫】数据提取之xpath
2022-07-04 22:46:00 【语音不识别】
安装
pip install lxml
导包
from lxml import etree
使用
将html字符串 转换为element对象
# 将html字符串 转换为element对象
from lxml import etree
element = etree.HTML(html_str)
以下是通过element对象.xpath('匹配规则')来提取内容
获取标签
使用 / 表示根节点,路径和路径之间的过渡
/html/xx/xx/xxx
使用 // 跨节点选取,直接到想要的标签或者文本
//xxx # 获取所有xxx标签
使用.
./ 当前节点
使用 ..
../ # 当前节点的上一级节点
.// 当不是完整的html时,使用 ,获取相对路径
获取属性
@属性名 获取当前标签 这个属性对应的属性值
//img/@src # 所有img 的scr属性
获取文本
/text() 获取标签里面的文本内容//标签名[contains( text() , ' 文字 ' ) ] 获取包含 文字的 标签
//ol/li//span[contains(text(),'可播放')]
获取特定条件标签
//标签名[@属性名=值] 根据标签的属性值去定位具体的标签
//span[@class='title'] # 可以通过类名进行获取了
//标签名[索引] 索引从1开始
从前边获取//上一级标签/标签名[position()>3] 从第4个开始
从后边获取//上一级标签/标签名[last()] 获取最后一个//上一级标签/标签名[last() - 2] 倒数第3个
结合//ol/li[position()>1][position()<last()-2]
//标签名[text()='值'] 根据标签中具体的文本内容定位具体的标签,需要一字不差进行匹配
//ol/li//span[text()='[可播放]'] # 匹配标签内容是[可播放]的标签
边栏推荐
- LabVIEW中比较两个VI
- SHP data making 3dfiles white film
- Install the gold warehouse database of NPC
- Attack and defense world misc advanced grace-50
- vim编辑器知识总结
- Redis入门完整教程:集合详解
- Redis introduction complete tutorial: client communication protocol
- Feature scaling normalization
- 剑指 Offer 65. 不用加减乘除做加法
- qt绘制网络拓补图(连接数据库,递归函数,无限绘制,可拖动节点)
猜你喜欢

Redis introduction complete tutorial: List explanation

The overview and definition of clusters can be seen at a glance

字体设计符号组合多功能微信小程序源码

Redis getting started complete tutorial: publish and subscribe

Attack and defense world misc advanced zone 2017_ Dating_ in_ Singapore

【室友用一局王者荣耀的时间学会了用BI报表数据处理】

Redis getting started complete tutorial: Geo

【机器学习】手写数字识别

Tweenmax emoticon button JS special effect

攻防世界 MISC 进阶区 can_has_stdio?
随机推荐
剑指 Offer 67. 把字符串转换成整数
金融市场,资产管理与投资基金
[sword finger offer] questions 1-5
智力考验看成语猜古诗句微信小程序源码
How can enterprises cross the digital divide? In cloud native 2.0
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
Redis入门完整教程:事务与Lua
Redis introduction complete tutorial: List explanation
mamp下缺少pcntl扩展的解决办法,Fatal error: Call to undefined function pcntl_signal()
【室友用一局王者荣耀的时间学会了用BI报表数据处理】
JS card style countdown days
剑指 Offer 65. 不用加减乘除做加法
Redis入门完整教程:Pipeline
Editplus-- usage -- shortcut key / configuration / background color / font size
Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
Redis入门完整教程:发布订阅
Gnawing down the big bone - sorting (II)
Redis:Redis的事务
时间 (计算)总工具类 例子: 今年开始时间和今年结束时间等
Redis: redis configuration file related configuration and redis persistence