1. 对响应数据进行修改编码为utf-8
2. 使用iso
#爬取图片:http://pic.netbian.com/4kdongwu/
url = 'http://pic.netbian.com/4kdongwu/'
response = requests.get(url=url,headers=headers)
#将响应数据的编码手动设定成了utf-8
# response.encoding = 'utf-8'
page_text = response.text
tree = etree.HTML(page_text)
li_list = tree.xpath('//div[@class="slist"]/ul/li')
for li in li_list:
img_name = li.xpath('./a/img/@alt')[0]+'.jpg'
#具有较强通用性乱码处理的方法
img_name = img_name.encode('iso-8859-1').decode('gbk')
img_src = 'http://pic.netbian.com'+li.xpath('./a/img/@src')[0]
request.urlretrieve(img_src,img_name)
print(img_name,'下载完毕')