解析JSON数据Python

2024-04-29 20:51:17 发布

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

所以我试图获取用户在Twitch上跟踪的频道列表

我使用urllib2从以下位置获取数据:

http://api.twitch.tv/kraken/users/user/follows/channels?offset=0&on_site=1

不过,我只想要用户跟踪的频道列表,这里是原始json数据的样子,我只想要每个频道的显示名称,例如['Syndicate','Gem28daz2012'],我如何做到这一点?在

{
    "follows": [{
        "created_at": "2016-01-15T22:43:35Z",
        "_links": {
            "self": "https://api.twitch.tv/kraken/users/user/follows/channels/syndicate"
        },
        "notifications": false,
        "channel": {
            "_links": {
                "self": "https://api.twitch.tv/kraken/channels/syndicate",
                "follows": "https://api.twitch.tv/kraken/channels/syndicate/follows",
                "commercial": "https://api.twitch.tv/kraken/channels/syndicate/commercial",
                "stream_key": "https://api.twitch.tv/kraken/channels/syndicate/stream_key",
                "chat": "https://api.twitch.tv/kraken/chat/syndicate",
                "features": "https://api.twitch.tv/kraken/channels/syndicate/features",
                "subscriptions": "https://api.twitch.tv/kraken/channels/syndicate/subscriptions",
                "editors": "https://api.twitch.tv/kraken/channels/syndicate/editors",
                "videos": "https://api.twitch.tv/kraken/channels/syndicate/videos",
                "teams": "https://api.twitch.tv/kraken/channels/syndicate/teams"
            },
            "background": null,
            "banner": null,
            "broadcaster_language": "en",
            "display_name": "Syndicate",
            "game": "Counter-Strike: Global Offensive",
            "logo": "https://static-cdn.jtvnw.net/jtv_user_pictures/syndicate-profile_image-03bf1d6ad0025f86-300x300.png",
            "mature": false,
            "status": "Road To MLG! (CS:GO Competiive) ",
            "partner": true,
            "url": "http://www.twitch.tv/syndicate",
            "video_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/syndicate-channel_offline_image-a15fc19d9828b9c7-640x360.png",
            "_id": 16764225,
            "name": "syndicate",
            "created_at": "2010-10-17T23:19:43Z",
            "updated_at": "2016-01-22T22:17:33Z",
            "delay": null,
            "followers": 2353424,
            "profile_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/syndicate-profile_banner-5f912a83f7e9bafe-480.png",
            "profile_banner_background_color": null,
            "views": 38140282,
            "language": "en"
        }
    }, {
        "created_at": "2016-01-12T21:01:49Z",
        "_links": {
            "self": "https://api.twitch.tv/kraken/users/user/follows/channels/gem28daz2012"
        },
        "notifications": false,
        "channel": {
            "_links": {
                "self": "http://api.twitch.tv/kraken/channels/gem28daz2012",
                "follows": "http://api.twitch.tv/kraken/channels/gem28daz2012/follows",
                "commercial": "http://api.twitch.tv/kraken/channels/gem28daz2012/commercial",
                "stream_key": "http://api.twitch.tv/kraken/channels/gem28daz2012/stream_key",
                "chat": "http://api.twitch.tv/kraken/chat/gem28daz2012",
                "features": "http://api.twitch.tv/kraken/channels/gem28daz2012/features",
                "subscriptions": "http://api.twitch.tv/kraken/channels/gem28daz2012/subscriptions",
                "editors": "http://api.twitch.tv/kraken/channels/gem28daz2012/editors",
                "videos": "http://api.twitch.tv/kraken/channels/gem28daz2012/videos",
                "teams": "http://api.twitch.tv/kraken/channels/gem28daz2012/teams"
            },
            "background": null,
            "banner": null,
            "broadcaster_language": null,
            "display_name": "Gem28daz2012",
            "game": "Minecraft",
            "logo": null,
            "mature": null,
            "status": "Playing minecraft",
            "partner": false,
            "url": "http://www.twitch.tv/gem28daz2012",
            "video_banner": null,
            "_id": 93338000,
            "name": "gem28daz2012",
            "created_at": "2015-06-12T19:51:58Z",
            "updated_at": "2016-01-12T21:16:07Z",
            "delay": null,
            "followers": 8,
            "profile_banner": null,
            "profile_banner_background_color": null,
            "views": 4,
            "language": "en"
        }
    }],
"_total": 2,
"_links": {
    "self": "https://api.twitch.tv/kraken/users/user/follows/channels?direction=DESC&limit=25&offset=0&sortby=created_at",
    "next": "https://api.twitch.tv/kraken/users/user/follows/channels?direction=DESC&limit=25&offset=25&sortby=created_at"
}

Tags: httpsapihttptvnullattwitchbanner
1条回答
网友
1楼 · 发布于 2024-04-29 20:51:17

要解析http响应体,可以使用^{}。之后,您可以使用列表压缩:

display_names = [f['channel']['display_name'] for f in data['follows']]

在某种背景下:

^{pr2}$

相关问题 更多 >