lxml提取html标签内容, tostring()不能显示中文 解决方案

from lxml import etree
import requests


response = requests.get('https://www.baisu.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")).decode("gbk") # 可以正常显示中文
print (strs)
发布了150 篇原创文章 · 获赞 149 · 访问量 81万+

猜你喜欢

转载自blog.csdn.net/chaishen10000/article/details/103168859
今日推荐