【问题标题】:python google api getting specific informationpython google api获取具体信息
【发布时间】:2013-01-04 14:40:49
【问题描述】:
#! /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 来完成它。但这行不通。那么如何获取特定信息呢?有什么简单的方法吗?

【问题讨论】:

    标签: python api search unicode


    【解决方案1】:

    From Google API Client documentation: "响应是从 JSON 响应构建的 Python 对象..."

    items的值是一个数组,所以需要指定要检索的元素的id:

    urlinfo = res['items'][0]['formattedUrl']
    

    【讨论】:

      猜你喜欢
      • 2013-01-15
      • 1970-01-01
      • 2014-11-16
      • 2016-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-31
      • 1970-01-01
      相关资源
      最近更新 更多