jjxx2005 发表于 2020-7-16 17:51:17

爬虫爬到的名称是乱码,请问如何解决,谢谢

import requests
from lxml import etree
import re
url ='http://www.1ppt.com/moban/shengri/'
UA = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36'}
r1 = requests.get(url=url,headers=UA).text
x1 = etree.HTML(r1)
fulr = x1.xpath('//ul[@class = "tplist"]//@href')
fulr = ['http://www.1ppt.com'+fulr for fulr in fulr]
fname = x1.xpath('//ul[@class = "tplist"]//@alt')
print(fname)



Twilight6 发表于 2020-7-16 17:56:46


设置下编码即可:r1.encoding = 'gbk',完整代码:

import requests
from lxml import etree
import re
url ='http://www.1ppt.com/moban/shengri/'
UA = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36'}
r1 = requests.get(url=url,headers=UA)
r1.encoding = 'gbk'
x1 = etree.HTML(r1.text)
fulr = x1.xpath('//ul[@class = "tplist"]//@href')
fulr = ['http://www.1ppt.com'+fulr for fulr in fulr]
fname = x1.xpath('//ul[@class = "tplist"]//@alt')
print(fname)

jjxx2005 发表于 2020-7-16 18:03:00

Twilight6 发表于 2020-7-16 17:56
设置下编码即可:r1.encoding = 'gbk',完整代码:

完全正确谢谢
页: [1]
查看完整版本: 爬虫爬到的名称是乱码,请问如何解决,谢谢