是否有一个简单的库可以连接到网站(例如:https://pixabay.com/)并过滤要下载的图像(并限制每个过滤器要下载的图像数量)?
答案 0 :(得分:1)
您可以使用 pixabay api。您需要采取以下步骤:
Your API key
https://pixabay.com/api/docs/#api_rate_limit
.您每小时可以发出 5,000 个请求。
安装pip install python-pixabay
示例代码
`
from pixabay import Image, Video
API_KEY = 'x'
image = Image(API_KEY)
image.search()
def download(url, pathname):
if not os.path.isdir(pathname):
os.makedirs(pathname)
response = requests.get(url, stream=True)
file_size = int(response.headers.get("Content-Length", 0))
filename = os.path.join(pathname, url.split("/")[-1])
progress = tqdm(response.iter_content(1024), f"Downloading {filename}", total=file_size, unit="B", unit_scale=True, unit_divisor=1024)
with open(filename, "wb") as f:
for data in progress.iterable:
f.write(data)
progress.update(len(data))
ims = image.search(q='cats dogs',lang='es',image_type='photo',orientation='horizontal',category='animals',safesearch='true',order='latest',page=2,per_page=3)
print(ims)
download(ims['hits'][0].get('previewURL'), 'photos')