除了直接读取字符串,还支持从文件读取内容。比如我们新建一个文件叫做 hello.html,内容为
first item second item third item fourth item fifth item
利用 parse 方法来读取文件。
from lxml import etree html = etree.parse('hello.html') result = etree.tostring(html, pretty_print=True) print(result)
同样可以得到相同的结果。
上一篇:python爬虫中lxml怎样测试
下一篇:python爬虫中lxml怎么使用?