Search code examples
pythonpython-3.ximage-manipulationgoogle-custom-search

How do i produce Unique result with Google's custom image in python


so Im using Google's Custom search api for python (3.6.6) to search for custom Images, i've been using this code:

from googleapiclient.discovery import build

import pprint

my_api_key = 'AIzaSyDouxn16TxJ8vym5AC1_A3nlMCzc5gpRjg'
my_cse_id = '007260579175343269764:pt3gyxmajmg'

def google_search(search_term, api_key, cse_id, **kwargs):
    service = build("customsearch", "v1", developerKey=api_key)
    res = service.cse().list(q=search_term, cx=cse_id, searchType="image", **kwargs).execute()
    return res['items']

results = google_search(
    'Mocha', my_api_key, my_cse_id)
pprint.pprint(results) 

but here's the problem, it returns the same search result everytime i use it, it returns the same result. I would like to ask; how do i make it return different search results every time i use the code?

P.S. I want the code to give me a different picture in the same search query, like when i use google images, I search Mocha its shows me a list of different images that have Mocha in the title, but the code here searches the query given, and then picks the first one, each time its used. so i want it to pick/show me a different one.


Solution

  • You get always the same output because you ask always the results for Mocha. The same happens when you put the same word in the Google search bar

    If you want to have different outputs you have to change the query that you perform. My approach would be to rely on a random word generator so to change the search every time you run the code

    Here I modified your own snippet to perform this exact job:

    from googleapiclient.discovery import build
    
    import pprint
    import requests
    import random
    
    my_api_key = 'AIzaSyDouxn16TxJ8vym5AC1_A3nlMCzc5gpRjg'
    my_cse_id = '007260579175343269764:pt3gyxmajmg'
    
    def google_search(search_term, api_key, cse_id, **kwargs):
        service = build("customsearch", "v1", developerKey=api_key)
        res = service.cse().list(q=search_term, cx=cse_id, searchType="image", **kwargs).execute()
        return res['items']
    
    def random_query():
        word_site = "http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain"
    
        response = requests.get(word_site)
        WORDS = response.content.splitlines()
        # print(WORDS)
    
        number = random.randint(0, len(WORDS));
        # print(number)
    
        word = WORDS[random.randint(0, len(WORDS))].decode()
        # print(word)
        return word
    
    def main():
        results = google_search(
            random_query(), my_api_key, my_cse_id)
        pprint.pprint(results) 
    
    if __name__ == "__main__":
        main()
    


    Edit after clarification in the comment

    In order to have different results for the same query you can pick the results from a page other than the first. The approach followed comes from this answer, you can tweak the start index as you prefer. In this snippet we select the page choosing a random number between 1 and 100. Keep in mind that every 10 units you have the next page, so as per the link given before "Second page has URL which contains start=10 parameter. Third page has URL which contains start=20", etc

    from googleapiclient.discovery import build
    
    import pprint
    import requests
    import random
    
    my_api_key = 'AIzaSyDouxn16TxJ8vym5AC1_A3nlMCzc5gpRjg'
    my_cse_id = '007260579175343269764:pt3gyxmajmg'
    
    def google_search(search_term, api_key, cse_id, start, **kwargs):
        service = build("customsearch", "v1", developerKey=api_key)
        res = service.cse().list(q=search_term, cx=cse_id, searchType="image", **kwargs, start=start).execute()
        return res['items']
        
    def random_start():
        number = random.randint(1,101)
        print(number)
        return number
    
    def main():
        start_index = random_start()
        results = google_search(
            'Mocha', my_api_key, my_cse_id, start_index)
        pprint.pprint(results) 
    
    if __name__ == "__main__":
        main()