Reputation: 43
#! /usr/bin/python
import pprint
from apiclient.discovery import build
def main():
service = build("customsearch", "v1",
developerKey="<mykey>")
res = service.cse().list(
q='the.hobbit.2012',
cx='<the other key>',
).execute()
pprint.pprint(res)
if __name__ == '__main__':
main()
Is the code i run and here is the result i get:
{u'context': {u'title': u'IMDB Search Engine'},
u'items': [{u'cacheId': u'kzgitw0HPeAJ',
u'displayLink': u'www.imdb.com',
u'formattedUrl': u'www.imdb.com/title/tt0903624/',
u'htmlFormattedUrl': u'www.imdb.com/title/tt0903624/',
u'htmlSnippet': u'Directed by Peter Jackson. With Martin Freeman, Ian McKellen, Richard Armitage<br> , Andy Serkis. A younger and more reluctant <b>Hobbit</b>, Bilbo Baggins, sets out on <b>...</b>',
u'htmlTitle': u'<b>The Hobbit</b>: An Unexpected Journey (<b>2012</b>) - IMDb',
u'kind': u'customsearch#result',
u'link': u'http://www.imdb.com/title/tt0903624/',
u'pagemap': {u'aggregaterating': [{u'bestrating': u'10',
u'ratingcount': u'157,307',
u'ratingvalue': u'8.4',
u'reviewcount': u'855'}],
Now my question is, if i wanted to get the information from u'formattedUrl' i thought i could do
urlinfo = res['item']['url']['formattedUrl']
like i would have done it with forexample Objconfig. But that wont work. So how do i get specific information out? Is there any easy way to do it?
Upvotes: 0
Views: 151
Reputation: 17693
From Google API Client documentation: "The response is a Python object built from the JSON response..."
The value of items
is an array, so you need to specify the id of the element you want to retrieve:
urlinfo = res['items'][0]['formattedUrl']
Upvotes: 1