如何从API请求中选择特定词典?

2024-09-26 18:17:43 发布

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

我可以从我的请求函数中获取字典。我得到的是:

{
    'collaborative': False,
    'external_urls': {
        'spotify': 'http://open.spotify.com/user/dashrif/playlist/3LEoetnegEv2Q8jdmB8TER'
    },
    'href': 'https://api.spotify.com/v1/users/dashrif/playlists/3LEoetnegEv2Q8jdmB8TER',
    'id': '3LEoetnegEv2Q8jdmB8TER',
    'images': [{
        'height': 640,
        'url': 'https://mosaic.scdn.co/640/0cd0508f78c5e5f6e2b01b3009753083c7977270527f35929eff151f80bcabec17b2fb9383da342b32d7d3432ff965abb01f706ec2efc38282a11b45d088e352f19eebb53874fcdc4366ff4249da45fe',
        'width': 640
    },
    {
        'height': 300,
        'url': 'https://mosaic.scdn.co/300/0cd0508f78c5e5f6e2b01b3009753083c7977270527f35929eff151f80bcabec17b2fb9383da342b32d7d3432ff965abb01f706ec2efc38282a11b45d088e352f19eebb53874fcdc4366ff4249da45fe',
        'width': 300
    },
    {
        'height': 60,
        'url': 'https://mosaic.scdn.co/60/0cd0508f78c5e5f6e2b01b3009753083c7977270527f35929eff151f80bcabec17b2fb9383da342b32d7d3432ff965abb01f706ec2efc38282a11b45d088e352f19eebb53874fcdc4366ff4249da45fe',
        'width': 60
    }],
    'name': 'Life Playlist Vol. I: The Fuck You Getting Hype For? You Still Broke',
    'owner': {
        'external_urls': {
            'spotify': 'http://open.spotify.com/user/dashrif'
        },
        'href': 'https://api.spotify.com/v1/users/dashrif',
        'id': 'dashrif',
        'type': 'user',
        'uri': 'spotify:user:dashrif'
    },
    'public': True,
    'snapshot_id': 'PCG8b/CxCfaCjX0mmFMZ3T9NUsJC1sz5MVAXfQf3aefQhcAi4Zdm2k+3rySb/HLw',
    'tracks': {
        'href': 'https://api.spotify.com/v1/users/dashrif/playlists/3LEoetnegEv2Q8jdmB8TER/tracks',
        'total': 63
    },
    'type': 'playlist',
    'uri': 'spotify:user:dashrif:playlist:3LEoetnegEv2Q8jdmB8TER'
}

至少我希望这是一本字典。老实说,在这一点上,我得到了这么多的错误,我不太确定。以下是相关代码:

playlist_api_endpoint = "{}/playlists".format(profile_data["href"])
playlists_response = requests.get(playlist_api_endpoint, 
headers=authorization_header)
playlist_data = json.loads(playlists_response.text)
display_arr = [profile_data] + playlist_data["items"] 
return render_template("index.html",sorted_array=display_arr)

基本上,我希望在添加新播放列表时能够过滤掉最后一个uri对象和任何其他uri对象。我尝试了.items()、筛选字典和其他一些我不记得的东西。如果有人知道我错在哪里或者如何实现我的目标,我希望得到一些帮助。谢谢!你知道吗


Tags: httpscomapiiddata字典uriplaylist

热门问题