如何从网站获取图片?

时间:2021-06-21 11:27:29

标签: python

  • 我想从网站下载图片(例如:https://pixabay.com/
  • 我想要填充要下载的图像(例如:“河流”或“房屋”)

是否有一个简单的库可以连接到网站(例如:https://pixabay.com/)并过滤要下载的图像(并限制每个过滤器要下载的图像数量)?

1 个答案:

答案 0 :(得分:1)

您可以使用 pixabay api。您需要采取以下步骤:

  1. 创建帐户 pixabay.com
  2. 转到此页面并获取 Your API key https://pixabay.com/api/docs/#api_rate_limit .

您每小时可以发出 5,000 个请求。

  1. 安装pip install python-pixabay

  2. 示例代码

`

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')