学无先后,达者为师

网站首页 编程语言 正文

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

作者:柴神 更新时间: 2022-02-05 编程语言

 

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)

 

原文链接:https://blog.csdn.net/chaishen10000/article/details/103168859

栏目分类
最近更新