arrrlo / Google-Images-Search

[PYTHON] Search for image using Google Custom Search API and resize & crop afterwards
MIT License
178 stars 34 forks source link

getting http error #110

Closed asultanoff closed 2 years ago

asultanoff commented 3 years ago

Hi, I'm trying this code:

from google_images_search import GoogleImagesSearch
apikey = '...'
projkey = '...'

gis = GoogleImagesSearch(apikey, projkey)

_search_params = {
    'q': 'puppy',
    'num': 10,
    'safe': None,
    'fileType': None,
    'imgType': None,
    'imgSize': None,
    'imgDominantColor': None,
    'rights': None

}
gis.search(search_params = _search_params)

and getting this error:

---------------------------------------------------------------------------
HttpError                                 Traceback (most recent call last)
<ipython-input-9-528c4e385abf> in <module>
     16 
     17 }
---> 18 gis.search(search_params = _search_params)
     19 
     20 

/opt/conda/envs/fastai/lib/python3.8/site-packages/google_images_search/fetch_resize_save.py in search(self, search_params, path_to_dir, width, height, custom_image_name, cache_discovery)
    149             self._search_params['num'] = num
    150 
--> 151             self._search_images(*self._get_data())
    152 
    153             if len(self._search_result) >= self._number_of_images \

/opt/conda/envs/fastai/lib/python3.8/site-packages/google_images_search/fetch_resize_save.py in _search_images(self, search_params, path_to_dir, width, height, cache_discovery)
    175         i = 0
    176         threads = []
--> 177         for url in self._google_custom_search.search(
    178             search_params, cache_discovery
    179         ):

/opt/conda/envs/fastai/lib/python3.8/site-packages/google_images_search/google_api.py in search(self, params, cache_discovery)
     77 
     78         search_params = self._search_params(params)
---> 79         res = self._query_google_api(search_params, cache_discovery)
     80 
     81         results = res.get('items', [])

/opt/conda/envs/fastai/lib/python3.8/site-packages/google_images_search/google_api.py in _query_google_api(self, search_params, cache_discovery)
     45                 cache_discovery=cache_discovery)
     46 
---> 47         return self._google_build.cse().list(
     48             cx=self._custom_search_cx, **search_params).execute()
     49 

/opt/conda/envs/fastai/lib/python3.8/site-packages/googleapiclient/_helpers.py in positional_wrapper(*args, **kwargs)
    132                 elif positional_parameters_enforcement == POSITIONAL_WARNING:
    133                     logger.warning(message)
--> 134             return wrapped(*args, **kwargs)
    135 
    136         return positional_wrapper

/opt/conda/envs/fastai/lib/python3.8/site-packages/googleapiclient/http.py in execute(self, http, num_retries)
    913             callback(resp)
    914         if resp.status >= 300:
--> 915             raise HttpError(resp, content, uri=self.uri)
    916         return self.postproc(resp, content)
    917 

HttpError: <HttpError 400 when requesting https://customsearch.googleapis.com/customsearch/v1?cx=52424915884&q=puppy&searchType=image&num=10&start=1&safe=off&key=...T_w&alt=json returned "Request contains an invalid argument.". Details: "Request contains an invalid argument.">
arrrlo commented 3 years ago

Hi @asultanoff,

Have you tried to debug it? Like removing keys in _search_params one by one and requesting image after each param removal?

Error message says "Request contains an invalid argument", so maybe it's ab out params.

For start, remove all None params.

chillingdino commented 3 years ago

@arrrlo im getting the same error with: gis.search({'q': 'puppies', 'num': 3}) like in the example

arrrlo commented 3 years ago

Hi @bcvent,

Did you manage to solve this? if not, can you please give me a complete exception trace.

Cheers!