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


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

从抽搐中我得到了一个 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' }字典要长得多

如何在不出错的情况下访问它

这是一个列表 你有它 像这样

使用 for 循环在它上面迭代

for i in data['streams']:
print(i['_id'] ,i['game'])

或选择索引

data["streams"][0]['_id']

相关内容

最新更新