https://pixabay.com python
时间: 2024-01-17 16:04:17 浏览: 117
python-pixabay:Python 3png的API包装器
根据提供的引用内容,以下是一个使用Python爬虫下载Pixabay网站图片的示例:
```python
import requests
import os
def download_image(url, save_path):
response = requests.get(url)
if response.status_code == 200:
with open(save_path, 'wb') as f:
f.write(response.content)
print("图片下载成功:", save_path)
else:
print("图片下载失败:", url)
def crawl_pixabay_images(keyword, num_images):
url = f"https://pixabay.com/images/search/{keyword}/"
response = requests.get(url)
if response.status_code == 200:
image_urls = response.json()["hits"][:num_images]
for i, image in enumerate(image_urls):
image_url = image["largeImageURL"]
save_path = f"{keyword}_{i+1}.jpg"
download_image(image_url, save_path)
else:
print("请求失败:", url)
crawl_pixabay_images("python", 5)
```
这段代码使用了requests库来发送HTTP请求,并通过解析返回的JSON数据获取图片的URL。然后,使用`download_image`函数下载图片并保存到本地。最后,调用`crawl_pixabay_images`函数来爬取指定关键字的图片。
阅读全文