我很长时间没有使用python了,我正试图通过一个网站从ESPN的梦幻足球网站上搜集一些信息。我使用的wesbite是https://stmorse.github.io/journal/espn-fantasy-v3.html。当我尝试下面列出的代码时,我得到KeyError:'schedule'。你知道吗
我试着完全按照网站上的代码,它给出了相同的错误。我打开了https://fantasy.espn.com/apis/v3/games/ffl/seasons/2019/segments/0/leagues/14380834?view=mMatchup上的api,看到有一个地方可以安排日程,所以不确定还需要做什么。你知道吗
league_id = 14380834
year = 2019
url = "https://fantasy.espn.com/apis/v3/games/ffl/seasons/2019/segments/0/leagues/14380834/"
r = requests.get(url)
d = r.json()
r = requests.get(url, params={"view": "mMatchup"})
df = [[
game['matchupPeriodId'],
game['home']['teamId'], game['home']['totalPoints'],
game['away']['teamId'], game['away']['totalPoints']
] for game in d['schedule']]
df = pd.DataFrame(df, columns=['Week', 'Team1', 'Score1', 'Team2', 'Score2'])
df['Type'] = ['Regular' if w<=14 else 'Playoff' for w in df['Week']]
df.head()
我的预期结果将是5个专栏,我呼吁一周得分2通过espn网站填充。你知道吗
回溯是:
File "C:/Users/jacob/PycharmProjects/FF_stuff/app.py", line 15, in <module>
] for game in d['schedule']]
KeyError: 'schedule'
Process finished with exit code 1
您需要从包含param的请求获取json
d
,而不是第一个请求。你知道吗这很管用
KeyError
的出现是因为没有参数的请求没有'schedule'
信息。您可以执行d.keys()
来查看dict
有哪些键相关问题 更多 >
编程相关推荐