通过www.bing.com/images 里的所有图片找到含有hydra关键词的图片并下载
import requests
from bs4 import BeautifulSoup
import os
# 创建一个文件夹来保存图片
if not os.path.exists('hydra_images'):
os.makedirs('hydra_images')
# Bing图片搜索URL
search_query = 'Hydra'
bing_search_url = f'https://www.bing.com/images/search?q={search_query}'
# 发送请求并获取网页内容
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
response = requests.get(bing_search_url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
# 找到所有图片标签
images = soup.find_all('img')
# 下载图片
for i, img in enumerate(images):
img_url = img.get('src')
if img_url and img_url.startswith('http'):
img_data = requests.get(img_url).content
with open(f'hydra_images/hydra_{i}.jpg', 'wb') as handler:
handler.write(img_data)
print("图片下载完成!")