通用的解决中文乱码的两种方法(爬虫)
第一种:对requests.get(url)获取的整体内容进行手动设定编码格式
response = requests.get(url)
response.encoding = ‘utf-8’
page_text = response.text
第二种:对出现乱码的变量进行手动设定编码
tree = etree.HTML(page_text)
li_list = tree.xpath(‘xpath表达式’)
for li in li_list:
img_name = li.xpath(‘xpath表达式’)
img_name = img_name.encode(‘iso-8859-1’).decode(‘gbk’)