[爬虫]lxml 获取当前节点的html,并正确显示中文

获取当前节点:etree.tostring

正确显示中文
方法一:html.unescape

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()中文乱码("数字;")解决方案
方法二:

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()不能显示中文 解决方案

你可能感兴趣的:(python)