Twitch API json字典错误列表索引必须是整数而不是str

2024-09-30 14:19:13 发布

您现在位置:Python中文网/ 问答频道 /正文

try:
    response = requests.get('https://api.twitch.tv/kraken/streams/followed', headers=headers)
    data = response.json()
except (KeyError, ValueError):
    print("Error - make sure your OAuth is formatted correctly in oauth.txt")
    sys.exit(1)
channelName = data["streams"]["channel"]["name"]
channelGame = data["streams"]["channel"]["game"]
channelViewers = str(data["streams"]["viewers"])
streamType = data["streams"]["stream_type"]
print(channelName, channelGame, channelViewers, streamType)

我得到的错误是列表索引必须是整数或片,而不是str

从twitch我得到了一个json字典:

{'streams': [{'_id': 2011610081, 'game': 'Sports & Fitness', 'broadcast_platform': 'live', 'community_id': '', 'community_ids': [], 'viewers': 12399, 'video_height': 900, 'average_fps':59, 'delay': 0, 'created_at': '2020-06-20T12:06:14Z', 'is_playlist': False, 'stream_type': 'live' } the dictionary is a lot longer

如何访问它而不出错


Tags: gamejsondataisresponsechannelstreamsheaders