Python 3.7 KeyError

问题描述 投票:0回答:2

我喜欢从NewsApi检索信息并遇到问题。附上代码:

from NewsApi import NewsApi
import pandas as pd
import os
import datetime as dt
from datetime import date


def CreateDF(JsonArray,columns):
    dfData = pd.DataFrame()

    for item in JsonArray:
        itemStruct = {}

        for cunColumn in columns:
            itemStruct[cunColumn] = item[cunColumn]

        # dfData = dfData.append(itemStruct,ignore_index=True)
            # dfData = dfData.append({'id': item['id'], 'name': item['name'], 'description': item['description']},
            #                        ignore_index=True)

    # return dfData
    return itemStruct

def main():
    # access_token_NewsAPI.txt must contain your personal access token
    with open("access_token_NewsAPI.txt", "r") as f:
        myKey = f.read()[:-1]
        #myKey = 'a847cee6cc254d8495632f83d5c77d39'

    api = NewsApi(myKey)

    # get sources of news
    # columns = ['id', 'name', 'description']
    # rst_source = api.GetSources()
    # df = CreateDF(rst_source['sources'], columns)
    # df.to_csv('source_list.csv')
    #
    #
    # # get news for specific country
    # rst_country = api.GetHeadlines()
    # columns = ['author', 'publishedAt', 'title', 'description','content', 'url']
    # df = CreateDF(rst_country['articles'], columns)
    # df.to_csv('Headlines_country.csv')

    # get  news for specific symbol
    symbol = "coronavirus"
    sources = 'bbc.co.uk'
    columns = ['author', 'publishedAt', 'title', 'description', 'content', 'source']
    limit = 500     # maximum requests per day
    i = 1
    startDate = dt.datetime(2020, 3, 1, 8)
    # startDate = dt.datetime(2020, 3, 1)
    df = pd.DataFrame({'author': [], 'publishedAt': [], 'title': [], 'description': [], 'content':[], 'source': []})
    while i < limit:
        endDate = startDate + dt.timedelta(hours=2)
        rst_symbol = api.GetEverything(symbol, 'en', startDate, endDate, sources)
        rst = CreateDF(rst_symbol['articles'], columns)
        df = df.append(rst, ignore_index=True)
        # DF.join(df.set_index('publishedAt'), on='publishedAt')
        startDate = endDate
        i += 1

    df.to_csv('Headlines_symbol.csv')

main()

我收到以下错误:

rst = CreateDF(rst_symbol['articles'], columns)
KeyError: 'articles'

在此行:

rst = CreateDF(rst_symbol['articles'], columns)

我认为关于找不到或定义密钥存在一些问题-有谁知道如何解决该问题?我很感谢每一个提示!

MAiniak

python keyerror
2个回答
0
投票

可能'articles'不是您在rst_symbol对象中的列之一。


0
投票

documentation没有提及任何名为NewsApi()GetEverything()的方法,而是NewsApiClient()get_everything(),即:

from newsapi import NewsApiClient

newsapi = NewsApiClient(api_key='xxx')
sources = newsapi.get_sources()
© www.soinside.com 2019 - 2024. All rights reserved.