The below block of code below is taken from the following forum Programmatically searching google in Python using custom search
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)
In the function google_search
I am having trouble understanding why and what happens when res
is returned as res['items']
verses just res
.
EDIT: Perhaps showing the result of changing of the two variants would help.
When res['items']
is used results
is a dictionary of containing 10 values each containing 11 items.
When just res
is used results
is a dictionary containing 6 items each each containing a different number of items and data structures.