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

Format of the q param #97

Closed itoche closed 3 years ago

itoche commented 3 years ago

Is there any specific format for the q param ? If I had several words like 'gartner magic quadrant', I end up with no results, which is weird as doing the same test with the API trial of https://developers.google.com/custom-search/v1/reference/rest/v1/cse/list provides the good results.

Please note that I used the CLI: gimages search -q 'gartner magic quadrant' -n 10

arrrlo commented 3 years ago

Hi @itoche,

That's strange because I'm getting results on that query just fine.

Here is my code sample:

api = GoogleImagesSearch('__api_key__', '__api_cx__')

_search_params = {
    'q': 'gartner magic quadrant',
    'num': 5
}
api.search(search_params=_search_params)

for image in self._api.results():
    print(image.url)

And here are the results: https://storage.googleapis.com/gweb-cloudblog-publish/images/Magic_Quadrant_full_life_cycle_api.max-2000x2000.jpg https://emtemp.gcom.cloud/ngw/commonassets/images/build-graphics/mq-preview.png https://storage.googleapis.com/gweb-cloudblog-publish/images/A-Gartner-Magic-Quadrant.max-2200x2200.jpg https://www.informatica.com/content/dam/informatica-com/en/image/misc/data-integration-magic-quadrant-2020.jpg https://storage.googleapis.com/gweb-cloudblog-publish/images/Magic_Quadrant_full_life_cycle_api.max-1100x1100.jpg

Let me know if the problem persists on your side.

Marcusg62 commented 3 years ago

Same Issue here, after query, the results list is empty.

itoche commented 3 years ago

Edited the original post to mention that i'm using the CLI. I will test with the code version provided by @arrrlo

itoche commented 3 years ago

So, the code example provided by. @arrrlo works fine. But the cli version is not working with the same query string. Strangely it works when the string contains 1 and 2 words but not 3 as in the aforementioned example.

Just an intuition but could it be that the @click.option('-q', '--query', help='Search query') contained in cli.py behave differently, maybe considering the list of words as an array instead of one unique string?

arrrlo commented 3 years ago

Ok, I've discovered where the problem was.

When using programmatic solution with _search_params = {'q': 'gartner magic quadrant', 'num': 5}, it reaches Google's lib with this set of final params:

{
    'q': 'gartner magic quadrant', 
    'searchType': 'image', 
    'num': 5, 
    'start': 1, 
    'safe': 'off'
}

And it gives you a fine result.

BUT! When using CLI, default params are set automatically, and the command gimages search -q 'gartner magic quadrant' -n 5 reaches Google's lib with this set of final params:

{
    'q': 'gartner magic quadrant', 
    'searchType': 'image', 
    'num': 5, 
    'start': 1, 
    'imgType': 'photo', 
    'imgSize': 'LARGE', 
    'fileType': 'jpg', 
    'safe': 'off', 
    'imgDominantColor': 'black', 
    'rights': 'cc_publicdomain'
}

And LARGE black jpg 'gartner magic quadrant' photo search ends in empty result by Google.

I've fixed this, CLI doesn't set any default params now. New lib version is 1.3.6

Thanks guys!