如果需求是m,那么如何从json获取2个键

2024-05-14 19:58:13 发布

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

我在做一个项目,它列出了Twitch上的前5个直播雷管。要创建这个列表,我想从状态为skyblock或moded且broadcast_platformlive的流中获取显示名称和查看器。但我还没弄明白怎么做。我是新来Python所以请不要烤我,如果真的很容易;)。提前谢谢你的帮助

我试过使用

names = [x['display_name'] for x in data['streams'][0]['channel'] if any(s in x.get('status', '').lower() for s in ['skyblock', 'modded']) ]

但这会产生以下错误:AttributeError: 'str' object has no attribute 'get'我不知道如何添加一种方法来检查broadcast_platform是否是live

'_total':115,
'streams':[  
  {  
     '_id':27188411728,
     'game':'Minecraft',
     'broadcast_platform':'live',
     'community_id':'227ee534-e395-4c02-b7e9-cc0160a7159c',
     'community_ids':[  
        '227ee534-e395-4c02-b7e9-cc0160a7159c'
     ],
     'viewers':503,
     'video_height':720,
     'average_fps':60.0220264317,
     'delay':0,
     'created_at':'2018-01-06T12:42:27Z',
     'is_playlist':False,
     'stream_type':'live',
     'preview':{  
        'small':'https://static-cdn.jtvnw.net/previews-ttv/live_user_lolddog-80x45.jpg',
        'medium':'https://static-cdn.jtvnw.net/previews-ttv/live_user_lolddog-320x180.jpg',
        'large':'https://static-cdn.jtvnw.net/previews-ttv/live_user_lolddog-640x360.jpg',
        'template':'https://static-cdn.jtvnw.net/previews-ttv/live_user_lolddog-{width}x{height}.jpg'
     },
     'channel':{  
        'mature':False,
        'status':'잉여맨 생방송 개꿀잼 아오오니 컨텐츠 (minecraft)',
        'broadcaster_language':'en',
        'display_name':'ㅁ잉여맨',
        'game':'Minecraft',
        'language':'ko',
        '_id':145971530,
        'name':'lolddog',
        'created_at':'2017-01-25T19:06:37.322554Z',
        'updated_at':'2018-01-06T14:06:35.661381Z',
        'partner':False,
        'logo':'https://static-cdn.jtvnw.net/jtv_user_pictures/a7439e1e92bae2cb-profile_image-300x300.png',
        'video_banner':'https://static-cdn.jtvnw.net/jtv_user_pictures/12d86926b1ed139e-channel_offline_image-1920x1080.png',
        'profile_banner':'https://static-cdn.jtvnw.net/jtv_user_pictures/lolddog-profile_banner-51fc562927fcd0a9-480.png',
        'profile_banner_background_color':'',
        'url':'https://www.twitch.tv/lolddog',
        'views':64203,
        'followers':6191,
        'broadcaster_type':'',
        'description':'잉여맨의 마인크래프트 신선하고 유쾌한 방송 입니다.'
     }
  },
  {  
     '_id':27187856336,
     'game':'Minecraft',
     'broadcast_platform':'live',
     'community_id':'0b9d06f9-fe5f-4f69-a4c3-2a9ee5604fb7',
     'community_ids':[  
        '0b9d06f9-fe5f-4f69-a4c3-2a9ee5604fb7',
        '227ee534-e395-4c02-b7e9-cc0160a7159c',
        'fd0eab99-832a-4d7e-8cc0-04d73deb2e54'
     ],
     'viewers':422,
     'video_height':900,
     'average_fps':60,
     'delay':0,
     'created_at':'2018-01-06T11:00:20Z',
     'is_playlist':False,
     'stream_type':'live',
     'preview':{  
        'small':'https://static-cdn.jtvnw.net/previews-ttv/live_user_matrixis-80x45.jpg',
        'medium':'https://static-cdn.jtvnw.net/previews-ttv/live_user_matrixis-320x180.jpg',
        'large':'https://static-cdn.jtvnw.net/previews-ttv/live_user_matrixis-640x360.jpg',
        'template':'https://static-cdn.jtvnw.net/previews-ttv/live_user_matrixis-{width}x{height}.jpg'
     },
     'channel':{  
        'mature':True,
        'status':'Weekend derping !prime - Modern Skyblock 2',
        'broadcaster_language':'en',
        'display_name':'Matrixis',
        'game':'Minecraft',
        'language':'en',
        '_id':32776386,
        'name':'matrixis',
        'created_at':'2012-08-06T17:03:31.398564Z',
        'updated_at':'2018-01-06T14:05:54.989584Z',
        'partner':True,
        'logo':'https://static-cdn.jtvnw.net/jtv_user_pictures/matrixis-profile_image-526891ecc78e1d4a-300x300.png',
        'video_banner':'https://static-cdn.jtvnw.net/jtv_user_pictures/matrixis-channel_offline_image-efea8977a5d238db-1920x1080.png',
        'profile_banner':'https://static-cdn.jtvnw.net/jtv_user_pictures/matrixis-profile_banner-3eb2813de7a4bfdb-480.png',
        'profile_banner_background_color':'',
        'url':'https://www.twitch.tv/matrixis',
        'views':1507379,
        'followers':77924,
        'broadcaster_type':'',
        'description':"I'm a Geek through and through. \rI'll talk about anything technical. \rComputer Engineer by trade. \rGamer for Life."
     }
  },

Tags: httpsidlivenetstaticcdnprofilejpg
1条回答
网友
1楼 · 发布于 2024-05-14 19:58:13

要根据频道状态获取显示名称:

names = [x['channel']['display_name'] for x in data['streams'] if any(s in x['channel'].get('status', '').lower() for s in ['skyblock', 'modded'])] 

根据频道状态和广播平台获取显示名称:

names = [x['channel']['display_name'] for x in data['streams'] if any(s in x['channel'].get('status', '').lower() for s in ['skyblock', 'modded']) and x['broadcast_platform'] == 'live']

相关问题 更多 >

    热门问题