from lxml import etree
import requests
response = requests.get('https://www.baidu.com/).text
tree = etree.HTML(response)
strs = tree.xpath( "//body")
strs = strs[0]
strs = (etree.tostring(strs)) # 不能正常显示中文
strs = (etree.tostring(strs, encoding = "utf-8", pretty_print = True, method = "html")) # 可以正常显示中文
print (strs)
lxml提取html标签内容, tostring()不能显示中文 解决方案
最新推荐文章于 2022-08-16 10:33:31 发布