解析来自YouTube API的JSON响应

GGA

我想保存一个对象的json响应并解析内容。我用代码调用了YouTube api请求

response = requests.get("https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet,statistics,topicDetails&chart=mostPopular&regionCode=IT&maxResults=25&key=MYAPIKEY")

然后我有一个200代码和一个成功的api响应。

接下来,我将json输出保存到名为data的新对象中

data = response.json()

现在,我正在尝试获取返回的每个视频的信息,以供后续sql存储。我正在尝试使用此代码

for item in data['items']:
    print('description')

我收到错误密钥错误:描述。我该如何解决?

普巴拉奈
for item in data['items']:
    print('description')

不是你想要的 print ('description')是打印文字字符串的指令description你可能是说print(item['description'])吗?这将使您可以将其item作为字典访问

或者可能是您错误地复制了代码。KeyError当您尝试访问不存在的字典键时发生A。例如:

>>> d = {'baz': 'foo'}
>>> d['quux']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'quux'

我将浏览您的数据,并确保您尝试访问的密钥确实存在。如果您使用有关API文档的更多信息更新您的问题,我们将为您提供更多帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章