立即学习:https://edu.csdn.net/course/play/24756/280673?utm_source=blogtoedu
XML库:
安装:PIP INSTALL LXML
lxm是一个HTML/XML的解析器,主要的功能是如何解析和提取HTML和XML数据。
使用方法1:
from lxml import etree
text='''
<div>
<ul>
<li class='item-0'><a href='link1.html'>frist item</a></li>
<li class='item-2'><a href='link1.html'>frist item</a></li>
<li class='item-3'><a href='link1.html'>frist item</a></li>
<li class='item-4'><a href='link1.html'>frist item</a></li>
<li class='item-5'><a href='link1.html'>frist item</a></li>
</ul>
</div>
'''
#将字符串解析为HTML文档
html=etree.HTML(text)
print(html)
#按字符串序列化HTML,就是转成字符串并且解码
result=etree.tostring(html).decode('utf-8')
print(result)
使用方法二:
html=etree.parse('sohu.html')
# html=etree.HTML(text)
print(html)
#按字符串序列化HTML,就是转成字符串并且解码
result=etree.tostring(html).decode('utf-8')
print(result)
存在问题,没找到读取文件路径