在unsplash网站上自动获得自己想要的种类的图片,并存储到指定文件夹
import requests
import os
import time
import random
#Unsplash API密钥
UNSPLASH_API_KEY = 'qugjL6Val_acmcYMA6Bxz4SK12lRW_-'#换成自己的密钥Access Key
# 创建一个目录来保存下载的图片
SAVE_DIR = 'bread_dish_images'
if not os.path.exists(SAVE_DIR):
os.makedirs(SAVE_DIR)
# 请求的参数
query = 'bread'#替换成自己想查找的图片类别
per_page = 30 # 每页数量
total_images = 1000 # 要获取的总图片数量
pages = total_images // per_page
# 设置最大重试次数
MAX_RETRIES = 3
for page in range(1, pages + 1):
# 构建API请求URL
url = f'https://api.unsplash.com/search/photos/?client_id={UNSPLASH_API_KEY}&query={query}&per_page={per_page}&page={page}'
response = requests.get(url)
data = response.json()
for item in data['results']:
image_url = item['urls']['regular']
image_id = item['id']
image_filename = f'{image_id}.jpg'
image_path = os.path.join(SAVE_DIR, image_filename)
retries = 0
success = False
while not success and retries < MAX_RETRIES:
try:
# 下载图片
image_response = requests.get(image_url)
image_response.raise_for_status() # 如果请求不成功会抛出异常
with open(image_path, 'wb') as f:
f.write(image_response.content)
print(f'Downloaded: {image_filename}')
success = True
except requests.exceptions.RequestException as e:
print(f'Error downloading image: {e}')
retries += 1
if retries < MAX_RETRIES:
print(f'Retrying ({retries}/{MAX_RETRIES})...')
# 添加随机延时,模拟人类访问行为
delay = random.uniform(1, 3) # 随机生成一个延时值,例如 1 到 3 秒之间
time.sleep(delay)
else:
print('Max retries reached. Moving on to the next image.')
print('Download complete!')