获取当前节点:etree.tostring
html
正确显示中文
方法一:html.unescape.net
from lxml import etree import html with open('list.html', 'r', encoding='utf-8') as f: text = f.read() tree = etree.HTML(text) r = html.unescape(etree.tostring(tree.xpath( '//*[@id="scroll_marquee"]')[0]).decode('utf-8')) print(r) print(type(r))
参考连接:爬取网页时调用tostring()中文乱码("数字;")解决方案
方法二:code
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)