直接给代码,可直接用,个人需要修改的地方有两处:
- self.directory 这是本地存储地址,修改为自己电脑的地址,另外,**{}**不要删
- spider.json_count = 10 这是下载的图像组数,一组有30张图像,10组就是三百张,根据需求下载
# -*- coding:utf8 -*-
import requests
import json
from urllib import parse
import os
import timeclass BaiduImageSpider(object):def __init__(self):self.json_count = 0 # 请求到的json文件数量(一个json文件包含30个图像文件)self.url = 'https://image.baidu.com/search/acjson?tn=resultjson_com&logid=5179920884740494226&ipn=rj&ct' \'=201326592&is=&fp=result&queryWord={' \'}&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&hd=&latest=©right=&word={' \'}&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=&nojc=&pn={' \'}&rn=30&gsm=1e&1635054081427= 'self.directory = r"D:\datasets\animal\兔子\{}" # 存储目录 这里需要修改为自己希望保存的目录 {}不要丢self.header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ''Chrome/95.0.4638.54 Safari/537.36 Edg/95.0.1020.30 '}# 创建存储文件夹def create_directory(self, name):self.directory = self.directory.format(name)# 如果目录不存在则创建if not os.path.exists(self.directory):os.makedirs(self.directory)self.directory += r'\{}'# 获取图像链接def get_image_link(self, url):list_image_link = []strhtml = requests.get(url, headers=self.header) # Get方式获取网页数据# jsonInfo = json.loads(strhtml.text)# for index in range(30):# list_image_link.append(jsonInfo['data'][index]['thumbURL'])# return list_image_linktry:jsonInfo = json.loads(strhtml.text)if 'data' in jsonInfo and isinstance(jsonInfo['data'], list):for item in jsonInfo['data']:if 'thumbURL' in item:list_image_link.append(item['thumbURL'])else:print(f"未找到有效数据,响应内容:{strhtml}")except json.JSONDecodeError as e:print(f"JSON解析错误: {e}, 响应内容:{strhtml}")except Exception as e:print(f"发生错误: {e}")return list_image_link# 下载图片def save_image(self, img_link, filename):res = requests.get(img_link, headers=self.header)if res.status_code == 404:print(f"图片{img_link}下载出错------->")with open(filename, "wb") as f:f.write(res.content)print("存储路径:" + filename)# 入口函数def run(self):searchName = input("查询内容:")searchName_parse = parse.quote(searchName) # 编码self.create_directory(searchName)pic_number = 0 # 图像数量for index in range(self.json_count):pn = (index+1)*30request_url = self.url.format(searchName_parse, searchName_parse, str(pn))list_image_link = self.get_image_link(request_url)for link in list_image_link:pic_number += 1self.save_image(link, self.directory.format(str(pic_number)+'.jpg'))time.sleep(0.2) # 休眠0.2秒,防止封ipprint(searchName+"----图像下载完成--------->")if __name__ == '__main__':spider = BaiduImageSpider()spider.json_count = 10 # 定义下载10组图像,也就是三百张spider.run()
学习自这位大佬,略有改动,目的是解决我遇到的 JSONDecodeError: Invalid \escape
错误,通常是由于 JSON 字符串中包含了无效的转义字符