Search code examples
pythongoogle-searchgoogle-search-api

Performing google search in python with API, returned with KeyError


I use exactly the same code in this answer, but it didn't work out.

from googleapiclient.discovery import build
import pprint

my_api_key = "Google API key"
my_cse_id = "Custom Search Engine ID"

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, **kwargs).execute()
    return res['items']

results = google_search(
    'stackoverflow site:en.wikipedia.org', my_api_key, my_cse_id, num=10)
for result in results:
    pprint.pprint(result)

The result shows KeyError: 'items'

Then I tried to remove the key and see what the result is.

It seems like there aren't any keys named "items"

So the question is:

How can I tweak the code and get a list of links ranked in the top 20 google search results?

Thanks in advance.

Sandra


Solution

  • This happens when the query has no result. If only it had results, it comes under res["items"]. Since you have no result, the items key is not generated.

    The custom search engine you created might be only accessible to very few URLs. Thus the result might be empty.

    Make sure your configuration for "Custom Search" in your search engine app located at Setup -> Basic (Tab) -> Sites to Search (Section) is set to "Search the entire web but emphasize include site".

    Also for the code, instead of returning res["items] directly, check if res["items"] is present, else return None. Then KeyError exception won't happen.

    if "items" in res.keys(): 
        return res["items"] 
    else: 
        return None