获取当前节点:etree.tostring

正确显示中文
方法一:使用html库的unescape函数
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()中文乱码("数字;")解决方案


方法二:使用lxml库的etree.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 = str(etree.tostring(info, encoding="utf-8"), encoding='utf-8')
print (strs)

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


universe_king
3.4k 声望677 粉丝

下一篇 »
MD5和Sha1