[Python]
来源:互联网 发布:windows 驱动 路径 编辑:程序博客网 时间:2024/06/16 10:50
lxml
用法源自 lxml python
官方文档,更多内容请直接参阅官方文档
lxml 官方文档
HTML 示例代码
text = '''<div> <ul> <li class="item-0"><a href="link1.html">first item</a></li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-inactive"><a href="link3.html">third item</a></li> <li class="item-1"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a> </ul> </div>'''
小试牛刀
使用 lxml
的 etree
库,将其打印出来
# 导入 lxml etree 库from lxml import etree# 获取 html 内容 元素htmlEmt = etree.HTML(text)# 将内容元素转换为字符串result = etree.tostring(htmlEmt)# utf-8 格式输出print(result.decode("utf-8"))
打印结果为:
<html><body><div> <ul> <li class="item-0"><a href="link1.html">first item</a></li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-inactive"><a href="link3.html">third item</a></li> <li class="item-1"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a></li> </ul> </div></body></html>
lxml 有自动修正 HTML 代码的功能
文件读取
利用 parse 方法来读取文件
文件名:text.xml
内容:
<div> <ul> <li class="item-0"><a href="link1.html">first item</a></li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-inactive"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-1"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a></li> </ul></div>
from lxml import etree# text.xml 是一个 xml 文件,并在当前文件同目录下htmlEmt = etree.parse('text.xml')# pretty_print: 优化输出result = etree.tostring(htmlEmt, pretty_print=True)# 输出print(result)
同样可以得到相同的结果
XPath实例测试
以上一段 text.xml
文件为例
获取所有的 <li>
标签
from lxml import etree# 获取文件元素htmlEmt = etree.parse('text.xml')# 获取所有的 <li> 标签 result = htmlEmt.xpath('//li')print(result)# 获取标签数量print(len(result))# 取出第一个 li 标签print(result[0])
输出结果:
[<Element li at 0x1023fc0c8>, <Element li at 0x1023fc108>, <Element li at 0x1023fc148>, <Element li at 0x1023fc188>, <Element li at 0x1023fc1c8>]5<Element li at 0x1023fc0c8>
获取 <li>
标签的所有 class
result = htmlEmt.xpath('//li/@class')print(result)
输出结果:
['item-0', 'item-1', 'item-inactive', 'item-1', 'item-0']
获取 <li>
标签下 href
为 link1.html
的 <a>
标签
result = htmlEmt.xpath('//li/a[@href="link1.html"]')print(result)print(result[0].text)
输出结果:
[<Element a at 0x1023fc208>]first item
获取 <li>
标签下的所有 <span>
标签
因为 /
是用来获取子元素的,而 <span>
并不是 <li>
的子元素,所以,要用双斜杠
result = html.xpath('//li//span')print(result)print(result[0].text)
输出结果:
[<Element span at 0x1023fc0c8>]third item
获取 <li>
标签下的所有 class
,不包括 <li>
标签的 class
result = html.xpath('//li/a//@class')print(result)
输出结果:
['bold']
获取最后一个 <li>
的 <a>
的 href
result = html.xpath('//li[last()]/a/@href')print(result)
输出结果:
['link5.html']
获取倒数第二个元素的内容
result = html.xpath('//li[last()-1]/a')print(result[0].text)
输出结果:
fourth item
获取 class
为 bold
的标签名
result = html.xpath('//*[@class="bold"]')print(result[0].tag)print(result[0].text)
输出结果:
spanthird item
阅读全文
0 0
- Python
- Python
- Python
- python
- Python
- PYTHON
- Python
- Python
- Python
- Python
- Python
- Python
- Python
- Python
- Python
- Python
- python
- Python
- mobkey api
- DNS笔记
- 最近工作好忙啊!!!
- JS点击事件删除单行数据
- mac常用终端命令
- [Python]
- Android人脸识别Demo竖屏YUV方向调整和图片保存
- SDUT 2711 4-2 电子时钟中的运算符重载
- Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quickstart:RELEASE
- jdk8 类和接口的多继承详解
- java线程池之小结
- 百度编辑器idea中 jsp运用
- monkeyrunnerdevice api
- Linux终极shell-Z Shell--用强大的zsh & oh-my-zsh把Bash换掉