如何使用Google自定义搜索API下载图像?

问题描述 投票:0回答:1

我在python中使用google image api下载了500张图像。下载一些图像后,它给出了错误的请求错误。下面是代码

from google_images_search import GoogleImagesSearch
import os
from tqdm import tqdm
import time


# Set up Google Images Search
gis = GoogleImagesSearch(API_KEY, API_SECRET, validate_images=False)

def download_images_in_batches(location, output_folder):
    search_params = {
        'q': location,
        'num': 50,  # Number of images to download per batch
        'fileType': 'jpg|png' # File types to include in the search
    }

    # Create the folder for the location if it doesn't exist
    location_folder = os.path.join(output_folder, location)
    os.makedirs(location_folder, exist_ok=True)

    # Download images in batches
    total_images = 500  # Total number of images to download
    images_per_batch = 50  # Number of images to download per batch
    batches = total_images // images_per_batch

    for batch in tqdm(range(batches)):
        start_index = batch * images_per_batch + 1
        search_params['start'] = start_index
        search_params['num'] = 50
        time.sleep(1)
        # Perform the search and download the images
        gis.search(search_params=search_params)

        for index, image in enumerate(gis.results()):
            if index >= images_per_batch:
                break
            image.download(location_folder)

        gis.next_page()

    print(f"Downloaded {total_images} images for {location} in folder '{location_folder}'")

download_images_in_batches('query_to_search', 'destination_path')

我想下载某个位置的 500 张图像,并且我正在批量下载。下载 200 张图像后,我收到以下错误

googleapiclient.errors.HttpError: <HttpError 400 when requesting https://customsearch.googleapis.com/customsearch/v1?cx=822fca83c44f645bb&q=US+Embassy+Baghdad&searchType=image&num=10&start=201&fileType=jpg%7Cpng&safe=off&key=AIzaSyD0pMnbiJmUnFactRxZvChEqY0i2G7gkFs&alt=json returned "Request contains an invalid argument.". Details: "[{'message': 'Request contains an invalid argument.', 'domain': 'global', 'reason': 'badRequest'}]">

我的 api 的限制是每天超过 500 个请求。谁能告诉我我哪里做错了?

python-3.x google-api google-api-python-client google-custom-search google-image-search
1个回答
0
投票

检查所使用的相应可编程搜索引擎(https://programmablesearchengine.google.com)返回的图像数量是多少。可能返回的图像较少。

浏览器上的 Google 搜索和可编程搜索引擎的结果计数不同。

我鼓励您直接查询 Google CSE,而不是使用 google_images_search 等 API。这是因为 Google CSE 文档明确指出要使用不同的搜索字符串进行查询,并每次查询下载 10 张图像。

在你的例子中,我会有 50 个不同的查询,每个查询生成 10 张图像,最后总共 500 张图像。您可以按照 Google CSE API 的文档制定不同的查询 (https://developers.google.com/custom-search/v1/reference/rest/v1/cse/list)。

© www.soinside.com 2019 - 2024. All rights reserved.