点击运行后:输入我们要搜的关键词
注意!!!!!!
self.directory 需要修改为自己保存的本地地址,本地地址 \{} 不要动 ,其它的就不需要修改了,我们搜索关键词是什么,那么我们的文件夹就是什么名字。
spider.json_count = 10 # 定义下载10组图像,也就是三百张
不需要这么多图片就把spider.json_count基数调小,看自己需求
import requests
import json
from urllib import parse
import os
import time
class 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"C:\Users\Lenovo\Desktop\{}" # 存储目录 这里需要修改为自己希望保存的目录 {}不要丢
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 = []
try:
response = requests.get(url, headers=self.header, timeout=10) # Get方式获取网页数据
response.raise_for_status() # 如果响应状态码不是200,则抛出异常
json_info = response.json()
for index in range(30):
list_image_link.append(json_info['data'][index]['thumbURL'])
except (requests.RequestException, ValueError, KeyError) as e:
print(f"Error occurred while fetching or parsing the JSON data: {e}")
return list_image_link
# 下载图片
def save_image(self, img_link, filename):
try:
response = requests.get(img_link, headers=self.header, timeout=10)
response.raise_for_status() # 如果响应状态码不是200,则抛出异常
with open(filename, "wb") as f:
f.write(response.content)
print("正在爬取: " + filename)
except requests.RequestException as e:
print(f"Error occurred while downloading the image: {e}")
# 入口函数
def run(self):
search_name = input("请输入你要搜索的关键词: ")
search_name_parse = parse.quote(search_name) # 编码
self.create_directory(search_name)
pic_number = 0 # 图像数量
for index in range(self.json_count):
pn = (index + 1) * 30
request_url = self.url.format(search_name_parse, search_name_parse, str(pn))
list_image_link = self.get_image_link(request_url)
for link in list_image_link:
pic_number += 1
self.save_image(link, self.directory.format(str(pic_number) + '.jpg'))
time.sleep(0.2) # 休眠0.2秒,防止封ip
print(search_name + " image download completed.")
if __name__ == '__main__':
spider = BaiduImageSpider()
spider.json_count = 10 # 定义下载10组图像,也就是三百张
spider.run()