【问题标题】:Select certain elements of response选择响应的某些要素
【发布时间】:2019-10-24 17:20:14
【问题描述】:

我发现自己不明白如何只选择我的 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}

【问题讨论】:

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


    【解决方案1】:

    问题是我只想选择活动流媒体的“用户名”列表
    ...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

    【讨论】:

    • 谢谢,伊万!工作得很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    相关资源
    最近更新 更多