爬虫 中文编码
'''
url = 'http://pic.netbian.com/4kdongwu/'
response = requests.get(url=url,headers=headers)
手动设定编码格式
response.encoding = 'utf-8'
page_text = response.text
tree = etree.HTML(page_text)
li_list = tree.xpath('//div[@class="slist"]/ul/li')
print(li_list)
import os
创建文件夹
if not os.path.exists('./img'):
os.mkdir('./img')
for li in li_list:
img_src = 'http://pic.netbian.com/'+li.xpath('./a/img/@src')[0]
img_name = li.xpath('./a/img/@alt')[0]+'.jpg'
img_name = img_name.encode('iso-8859-1').decode('gbk')
# print(img_name)
# 请求图片 存储 获取二进制文件
img_data = requests.get(url=img_src,headers=headers).content
img_path = './img/'+img_name
with open(img_path,'wb') as fp:
fp.write(img_data)
print('下载成功')
'''
浙公网安备 33010602011771号