选择回应的某些要素

时间:2019-06-10 09:58:46

标签: python python-3.x python-requests

我发现自己不理解如何仅选择Steam API请求响应中的某些元素。

这是带有结果的代码,可以在Steam上发出正确的请求。这是不可复制的,因为client_id是个人信息。结果包括在内。

# All online streamers
client_id = "...confidential"
limit = "2"
def request_dataNewAPI(limit):
    headers = {"Client-ID": client_id, "Accept": "application/vnd.twitchtv.v5+json"}
    url = "https://api.twitch.tv/helix/streams?first=" + limit
    r = requests.get(url, headers=headers).json()
    return r
    # If a bad user login name or offline response will be:
    # {'data': [], 'pagination': {}}

table1 = request_dataNewAPI(limit)

输出为:

New API 

{'data': [{'id': '34472839600', 'user_id': '12826', 'user_name': 'Twitch', 'game_id': '509663', 'community_ids': ['f261cf73-cbcc-4b08-af72-c6d2020f9ed4'], 'type': 'live', 'title': 'The 1st Ever 3rd or 4th Pre Pre Show! Part 6', 'viewer_count': 19555, 'started_at': '2019-06-10T02:01:20Z', 'language': 'en', 'thumbnail_url': 'https://static-cdn.jtvnw.net/previews-ttv/live_user_twitch-{width}x{height}.jpg', 'tag_ids': ['d27da25e-1ee2-4207-bb11-dd8d54fa29ec', '6ea6bca4-4712-4ab9-a906-e3336a9d8039']}, {'id': '34474693232', 'user_id': '39298218', 'user_name': 'dakotaz', 'game_id': '33214', 'community_ids': [], 'type': 'live', 'title': '????????? ???? | code: dakotaz in itemshop & GFUEL', 'viewer_count': 15300, 'started_at': '2019-06-10T06:37:02Z', 'language': 'en', 'thumbnail_url': 'https://static-cdn.jtvnw.net/previews-ttv/live_user_dakotaz-{width}x{height}.jpg', 'tag_ids': ['6ea6bca4-4712-4ab9-a906-e3336a9d8039']}], 'pagination': {'cursor': 'eyJiIjpudWxsLCJhIjp7Ik9mZnNldCI6Mn19'}}

问题是我只想选择活动拖缆的“用户名”列表。我尝试了以下方法:

print(table1['data']['user_name'])

给出“ TypeError:列表索引必须是整数或切片,而不是str”。

print(table1['data'])

提供整个数据数组:

[{'id': '34472839600', 'user_id': '12826', 'user_name': 'Twitch', 'game_id': '509663', 'community_ids': ['f261cf73-cbcc-4b08-af72-c6d2020f9ed4'], 'type': 'live', 'title': 'The 1st Ever 3rd or 4th Pre Pre Show! Part 6', 'viewer_count': 19555, 'started_at': '2019-06-10T02:01:20Z', 'language': 'en', 'thumbnail_url': 'https://static-cdn.jtvnw.net/previews-ttv/live_user_twitch-{width}x{height}.jpg', 'tag_ids': ['d27da25e-1ee2-4207-bb11-dd8d54fa29ec', '6ea6bca4-4712-4ab9-a906-e3336a9d8039']}, {'id': '34474693232', 'user_id': '39298218', 'user_name': 'dakotaz', 'game_id': '33214', 'community_ids': [], 'type': 'live', 'title': '????????? ???? | code: dakotaz in itemshop & GFUEL', 'viewer_count': 15300, 'started_at': '2019-06-10T06:37:02Z', 'language': 'en', 'thumbnail_url': 'https://static-cdn.jtvnw.net/previews-ttv/live_user_dakotaz-{width}x{height}.jpg', 'tag_ids': ['6ea6bca4-4712-4ab9-a906-e3336a9d8039']}]

作为最后的结果,我想做些类似的事情:

'user_name': {name1, name2}

1 个答案:

答案 0 :(得分:0)

  

问题是我只想选择活动拖缆的“用户名”列表
  ... print(table1['data']['user_name'])
  ...给出"TypeError: list indices must be integers or slices, not str"

您收到TypeError是因为table1['data']list,而不是dict,并且您必须使用int而不是str访问其成员(尽管dict键也可以是int

使用list comprehension

user_names = [x['user_name'] for x in table1['data']]

这将为您提供代表用户名的字符串list