from lxml import etree
import requests
import os
# 爬取彼岸图库中的图片数据
if __name__ == '__main__':
#爬取到页面源码数据
url = 'https://pic.netbian.com/4kmeinv/'
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'
}
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')
if not os.path.exists('./pictureB'):
os.mkdir('./pictureB')
for li in li_list:
img_src = 'https://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')
img_data = requests.get(url=img_src, headers=headers).content
with open('./pictureB/'+img_name, 'wb') as fp:
fp.write(img_data)
print(img_name, '爬取成功')
标签:__,name,img,图库,彼岸,li,爬取,headers
From: https://www.cnblogs.com/lin513/p/18034947