0
#! /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()

私が実行したコードです。ここに私が得た結果があります:

{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'}],

今私の質問は、u'formattedUrl' から情報を取得したい場合、私はできると思った

urlinfo = res['item']['url']['formattedUrl']

たとえば、Objconfig でそれを行ったように。しかし、それはうまくいきません。では、具体的な情報を得るにはどうすればよいでしょうか。それを行う簡単な方法はありますか?

4

1 に答える 1

1

Google API クライアントのドキュメントから:「応答は、JSON 応答から構築された Python オブジェクトです...」

の値itemsは配列であるため、取得する要素の ID を指定する必要があります。

urlinfo = res['items'][0]['formattedUrl']
于 2013-01-04T16:32:18.713 に答える