【问题标题】:Parsing a python list converted from JSON解析从 JSON 转换的 python 列表
【发布时间】:2016-05-25 09:35:59
【问题描述】:

我正在尝试使用谷歌自定义搜索 API 来搜索美国新闻媒体。使用 google 提供的代码示例,您最终会得到一个包含大量其他字典和列表的 python 字典。元函数中“res”旁边列出的标签是我试图为每篇文章访问的值。

import os.path
import csv
from lxml import html
from googleapiclient.discovery import build


def newslist():
  '''
  Uses google custom search to search 20 US news sources for gun control articles,
  and converts info into python dictionary.

  in - none

  out - res: JSON formatted search results

  '''

  service = build("customsearch", "v1",
            developerKey="key")
  res = service.cse().list(
      q='query',
      cx='searchid',
    ).execute()
  return res


def meta(res, doc_count):
  '''
  Finds necessary meta-data of all articles. Avoids collections, such as those found on Huffington Post and New York Times.

  in - res: defined above

  out - meta_csv: csv file with article meta-data

  '''

  row1 = ['doc_id', 'url', 'title', 'publisher', 'date']
  if res['context']['items']['pagemap']['metatags']['applicationname'] is not 'collection':
    for art in res['context']['items']:
      url = res['context']['items']['link']
      title = res['context']['items']['pagemap']['article']['newsarticle']['headline']
      publisher = res['context']['items']['displayLink'].split('www.' and '.com')
      date = res['context']['items']['pagemap']['newsarticle']['datepublished']
      row2 = [doc_count, url, title, publisher, date]
      with open('meta.csv', 'w', encoding = 'utf-8') as meta:
        csv_file = csv.writer(meta, delimiter = ',', quotechar = '|',
          quoting = csv.QUOTE_MINIMAL)
        if doc_count == 1:
          csv_file.writerow(row1)
        csv_file.writerow(row2)
  doc_count += 1

以下是搜索查询的打印输出示例:

{'context': {'title': 'Gun Control articles'},
 'items': [{'displayLink': 'www.washingtonpost.com',
    'formattedUrl': 'https://www.washingtonpost.com/.../white-resentment-is-fueling-opposition- '
                    'to-gun-control-researchers-say/',
    'htmlFormattedUrl': 'https://www.washingtonpost.com/.../white-resentment-is-fueling-opposition- '
                        'to-<b>gun</b>-<b>control</b>-researchers-say/',
    'htmlSnippet': 'Apr 4, 2016 <b>...</b> Racial prejudice could play '
                   'a significant role in white Americans&#39; '
                   'opposition to <br>\n'
                   '<b>gun control</b>, according to new research from '
                   'political scientists at&nbsp;...',
    'htmlTitle': 'White resentment is fueling opposition to <b>gun '
                 'control</b>, researchers say',
    'kind': 'customsearch#result',
    'link': 'https://www.washingtonpost.com/news/wonk/wp/2016/04/04/white-resentment-is-fueling-opposition-to-gun-control-researchers-say/',
    'pagemap': {'cse_image': [{'src': 'https://img.washingtonpost.com/rf/image_1484w/2010-2019/WashingtonPost/2015/10/03/Others/Images/2015-10-03/Botsford_gunshow1004_15_10_03_41831443897980.jpg'}],
                'cse_thumbnail': [{'height': '183',
                                   'src': 'https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSXtMnfm_GHkX3d2dOWgmto3rFjmhzxV8InoPao1tBuiBrEWsDMz4WDKcPB',
                                   'width': '275'}],
                'metatags': [{'apple-itunes-app': 'app-id=938922398, '
                                                  'app-argument=https://www.washingtonpost.com/news/wonk/wp/2016/04/04/white-resentment-is-fueling-opposition-to-gun-control-researchers-say/',
                              'article:author': 'https://www.facebook.com/chrisingraham',
                              'article:publisher': 'https://www.facebook.com/washingtonpost',
                              'author': 'https://www.facebook.com/chrisingraham',
                              'fb:admins': '1513210492',
                              'fb:app_id': '41245586762',
                              'news_keywords': 'guns, gun control, '
                                               'racial resentment, '
                                               'white people',
                              'og:description': 'Some white gun owners '
                                                '"understand '
                                                "'freedom' in a very "
                                                'particular way."',
                              'og:image': 'https://img.washingtonpost.com/rf/image_1484w/2010-2019/WashingtonPost/2015/10/03/Others/Images/2015-10-03/Botsford_gunshow1004_15_10_03_41831443897980.jpg',
                              'og:site_name': 'Washington Post',
                              'og:title': 'White resentment is fueling '
                                          'opposition to gun control, '
                                          'researchers say',
                              'og:type': 'article',
                              'og:url': 'https://www.washingtonpost.com/news/wonk/wp/2016/04/04/white-resentment-is-fueling-opposition-to-gun-control-researchers-say/',
                              'referrer': 'unsafe-url',
                              'twitter:card': 'summary_large_image',
                              'twitter:creator': '@_cingraham',
                              'viewport': 'width=device-width, '
                                          'initial-scale=1.0, '
                                          'user-scalable=yes, '
                                          'minimum-scale=0.5, '
                                          'maximum-scale=2.0'}],
                'newsarticle': [{'articlebody': 'People look at '
                                                'handguns during the '
                                                "Nation's Gun Show in "
                                                'Chantilly, Va. in '
                                                'October 2015. (Photo '
                                                'by Jabin Botsford/The '
                                                'Washington Post) '
                                                'Racial prejudice '
                                                'could play a '
                                                'significant role in '
                                                'white...',
                                 'datepublished': '2016-04-04T11:46-500',
                                 'description': 'Some white gun owners '
                                                '"understand '
                                                "'freedom' in a very "
                                                'particular way."',
                                 'headline': 'White resentment is '
                                             'fueling opposition to '
                                             'gun control, researchers '
                                             'say',
                                 'image': 'https://img.washingtonpost.com/rf/image_1484w/2010-2019/WashingtonPost/2015/10/03/Others/Images/2015-10-03/Botsford_gunshow1004_15_10_03_41831443897980.jpg',
                                 'mainentityofpage': 'True',
                                 'url': 'https://www.washingtonpost.com/news/wonk/wp/2016/04/04/white-resentment-is-fueling-opposition-to-gun-control-researchers-say/'}],
                'person': [{'name': 'Christopher Ingraham'}]},
    'snippet': 'Apr 4, 2016 ... Racial prejudice could play a '
               "significant role in white Americans' opposition to \n"
               'gun control, according to new research from political '
               'scientists at\xa0...',
    'title': 'White resentment is fueling opposition to gun control, '
             'researchers say'},

我知道我基本上可以编写一个 for 循环,但我想知道是否有一种更简单、代码量更少的方法来访问每个所需值的数据:URL、标题、发布者和日期。

【问题讨论】:

    标签: python json list dictionary google-api-python-client


    【解决方案1】:

    为什么不使用json 模块?

    import json
    s = ... # Your JSON text
    result = json.loads(s)
    

    result 将是 dictlist,具体取决于您的 JSON。

    【讨论】:

    • 我已经尝试过了,但收到一个错误,提示 json.loads 无法解析字典。我发现 google 的 python api 将其原始 json 输出转换为列表和字典的字典,如上所示。由于 pprint,它被格式化为 json,但上面显示的输出由 python 列表和字典组成。
    • json.load(s) CAN 解析字典。请使用 json.loads() 上的错误更新您的问题。
    猜你喜欢
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    相关资源
    最近更新 更多