user3206897
user3206897

Reputation: 11

How to extract number of search results from Google's Custom Search API?

For my research project (in social sciences) I would like to extract the total number of hits of a specific keyword in a specific website from Google's CSE API. I am 'using' python for the first time, I will try my best to be clear..

import pprint

from apiclient.discovery import build

def main():
  service = build("customsearch", "v1",
        developerKey="<my_key>")
  res = service.cse().list(
    q='greenpeace',
    cx='<other_key>',
    siteSearch='www.foe.org',
    fields='searchInformation'
    ).execute()
    pprint.pprint(res)

if __name__ == '__main__':
main()

I get the following result when running it in my terminal:

{u'searchInformation': {u'formattedSearchTime': u'0.12',
                    u'formattedTotalResults': u'37',
                    u'searchTime': 0.124824,
                    u'totalResults': u'37'}}

How do I extract the number of total results 37 in this case in the form of a variable? I found out already how to save variables in a csv, which is my ultimate goal. If there is another way of saving this number in a csv, that's fine as well. I will have to perform more of these searches, by reading out keywords and domain from a csv and saving the total number of hits next to it...

Upvotes: 1

Views: 1377

Answers (1)

Richard
Richard

Reputation: 161

What you have in your res variable is a Python dictionary whose first key ('searchInformation') has for its value another dictionary in which the data you want is at the key 'totalResults'.

total_results = res['searchInformation']['totalResults']

Upvotes: 1

Related Questions