一、注意事项
1、抓取图片的代码较简单,有些网站拿到文字乱码,主要体现转化乱码的操作,encode(‘iso-8859-1’).decode(‘gbk’);
2、用xpath进行标签解析
二、源码
import requests
from lxml import etree
import os
headers = {'User-Agent': 'Mozilla/5.0'}
file_path = './pic'
if not os.path.exists(file_path):
os.makedirs(file_path)
for page in range(1, 6):
if page == 1:
url = 'http://pic.netbian.com/4kqiche/index.html'
else:
url = f'http://pic.netbian.com/4kqiche/index_{page}.html'
resp = requests.get(url, headers=headers).text
tree = etree.HTML(resp)
li_list = tree.xpath('//ul[@class="clearfix"]/li')
for li in li_list:
img_src = 'http://pic.netbian.com' + li.xpath('./a/img/@src')[0]
img_name = li.xpath('./a/b/text()')[0]
img_name = img_name.encode('iso-8859-1').decode('gbk')
img_save = requests.get(img_src, headers=headers).content
with open(f'{file_path}/{img_name}.jpg', 'wb') as fp:
fp.write(img_save)
感谢看到这里