# --coding:utf-8--
from lxml import etree
str = r'''<html><head><title>测试</title></head><body><div>
<ul><li id="1">1111111</li><li id="2">2222222</li>
<li id="3">3333333</li></ul></div></body></html>'''
# 解析html字符串
html = etree.HTML(str) # 将字符串转换为HTML对象(<Element html at 0x2e1fdd055c0>)
result = etree.tostring(html).decode('utf-8') # 将html对象转换成字符串
# 解析html文件
html = etree.parse('test.html') # 将html文件装载到html对象中
result = etree.tostring(html).decode('utf-8')
print(result)
1、etree.HTML(str) :将字符串转换为HTML对象(<Element html at 0x2e1fdd055c0>)
2、etree.tostring(html).decode('utf-8') :将html对象转换成字符串
3、etree.parse('htmlfile') :将html文件装载到html对象中