lxml xpath 爬取猫眼电影出现的编码问题【Python3网络爬虫开发实战教程 崔庆才】

import requests
from lxml import etree

headers = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) '
                          'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36'
        }

url = 'https://maoyan.com/board/4'

response = requests.get(url, headers=headers)

# 如果不手动设置一个带编码方案的解析器,可能会出现乱码
html = etree.HTML(response.content, parser=etree.HTMLParser(encoding='utf8'))

# 转为字符串时一定要声明编码为utf-8,否则无法正常显示中文,而是显示原编码的字符,并没有正确转换。
result = etree.tostring(html, encoding="utf-8", pretty_print=True, method="html")

print(result.decode('utf-8'))

你可能感兴趣的:(爬虫,Python,lxml,python,xpath,爬虫,编码)