从Spotify API请求

时间:2018-06-02 17:57:16

标签: python spotify spotipy

Heyo,我需要帮助从使用Spotipy调用Spotify API中获取正确的信息。我对Python比较陌生,所以也许我错过了一些非常明显的东西,但我已经研究了几个小时并尝试了我能想到的一切,但仍然没有得到我想要的结果。我基本上想要问Spotify一个用户保存的库,并从响应中取出曲目标题和艺术家并保存以供日后使用。目前我打电话

tracks_response = spotify.current_user_saved_tracks(limit=1, offset=0)
print(tracks_response) 

我收到这样的话:

 {'href': 'https://api.spotify.com/v1/me/tracks?offset=0&limit=1', 'items': 
 [{'added_at': '2018-05-31T18:59:42Z', 'track': {'album': {'album_type': 
 'album', 'artists': [{'external_urls': {'spotify': 
 'https://open.spotify.com/artist/4AA8eXtzqh5ykxtafLaPOi'}, 'href': 
 'https://api.spotify.com/v1/artists/4AA8eXtzqh5ykxtafLaPOi', 'id': 
 '4AA8eXtzqh5ykxtafLaPOi', 'name': 'What So Not', 'type': 'artist', 'uri': 
 'spotify:artist:4AA8eXtzqh5ykxtafLaPOi'}], 'available_markets': [...], 
 'external_urls': {'spotify': 
 'https://open.spotify.com/album/3XaUNjcSJ6oyhoaFTqRbLb'}, 'href': 
 'https://api.spotify.com/v1/albums/3XaUNjcSJ6oyhoaFTqRbLb', 'id': 
 '3XaUNjcSJ6oyhoaFTqRbLb', 'images': [{'height': 640, 'url': 
 'https://i.scdn.co/image/3a6afacbac6870ec6025694f4f7d931fe3d0e383', 
 'width': 640}, {'height': 300, 'url': 
 'https://i.scdn.co/image/64e4b200281890ec57c239591666b31eba03e6b8', 
 'width': 300}, {'height': 64, 'url': 
 'https://i.scdn.co/image/f217a511095e82b4a34dbaa379e2d58b4f9fe9ef', 
 'width': 64}], 'name': 'Divide & Conquer (Remixes)', 'release_date': '2017- 
  06-16', 'release_date_precision': 'day', 'type': 'album', 'uri': 
 'spotify:album:3XaUNjcSJ6oyhoaFTqRbLb'}, 'artists': [{'external_urls': 
 {'spotify': 'https://open.spotify.com/artist/4AA8eXtzqh5ykxtafLaPOi'}, 
 'href': 'https://api.spotify.com/v1/artists/4AA8eXtzqh5ykxtafLaPOi', 'id': 
 '4AA8eXtzqh5ykxtafLaPOi', 'name': 'What So Not', 'type': 'artist', 'uri': 
 'spotify:artist:4AA8eXtzqh5ykxtafLaPOi'}, {'external_urls': {'spotify': 
 'https://open.spotify.com/artist/6sUbCA1obWzdoJ1FM3I9Ks'}, 'href': 
 'https://api.spotify.com/v1/artists/6sUbCA1obWzdoJ1FM3I9Ks', 'id': 
 '6sUbCA1obWzdoJ1FM3I9Ks', 'name': 'GANZ', 'type': 'artist', 'uri':  
 'spotify:artist:6sUbCA1obWzdoJ1FM3I9Ks'}, {'external_urls': {'spotify': 
 'https://open.spotify.com/artist/2sphpXGg1u97VSzbm5gR2P'}, 'href': 
 'https://api.spotify.com/v1/artists/2sphpXGg1u97VSzbm5gR2P', 'id': 
 '2sphpXGg1u97VSzbm5gR2P', 'name': 'JOY.', 'type': 'artist', 'uri': 
 'spotify:artist:2sphpXGg1u97VSzbm5gR2P'}, {'external_urls': {'spotify': 
 'https://open.spotify.com/artist/3C1R0cMeIfSFxpAFxCh5yQ'}, 'href': 
 'https://api.spotify.com/v1/artists/3C1R0cMeIfSFxpAFxCh5yQ', 'id': 
 '3C1R0cMeIfSFxpAFxCh5yQ', 'name': 'Slow Hours', 'type': 'artist', 'uri': 
 'spotify:artist:3C1R0cMeIfSFxpAFxCh5yQ'}], 'available_markets': [...], 
 'disc_number': 1, 'duration_ms': 272999, 'explicit': False, 'external_ids': 
 {'isrc': 'AUDCB1600994'}, 'external_urls': {'spotify': 
 'https://open.spotify.com/track/3c5Og78p3plOCBbNLg5K9L'}, 'href': 
 'https://api.spotify.com/v1/tracks/3c5Og78p3plOCBbNLg5K9L', 'id': 
 '3c5Og78p3plOCBbNLg5K9L', 'name': 'Lone (feat. JOY.) - Slow Hours Remix', 
 'popularity': 53, 'preview_url': 'https://p.scdn.co/mp3- 
 preview/cc865407083054f5be6f1adbe9eb93295e53c670? 
 cid=6bf27521c6ff4eb2bf72698c63a1d9e8', 'track_number': 2, 'type': 'track', 
 'uri': 'spotify:track:3c5Og78p3plOCBbNLg5K9L'}}], 'limit': 1, 'next': 
 'https://api.spotify.com/v1/me/tracks?offset=1&limit=1', 'offset': 0, 
 'previous': None, 'total': 1369}

我认为这是一个普通的字典,所以我很容易通过使用普通的字典语法来获得艺术家,如

 print(tracks_response['items']['track']['artists']['name'])

但是当我打电话给我收到错误报告时:

 TypeError: list indices must be integers or slices, not str

但如果我将通话更改为:

 print(tracks_response['items'])

我明白了:

 [{'added_at': '2018-05-31T18:59:42Z', 'track': {'album': {'album_type': 
 'album', 'artists': [{'external_urls': {'spotify': 
 'https://open.spotify.com/artist/4AA8eXtzqh5ykxtafLaPOi'}, 'href': 
 'https://api.spotify.com/v1/artists/4AA8eXtzqh5ykxtafLaPOi', 'id': 
 '4AA8eXtzqh5ykxtafLaPOi', 'name': 'What So Not',...

与上述内容相同,但从'项目开始#39; key ...看起来字典的其余部分是包含键值对的列表,我如何从中获取我想要的数据?

感谢任何帮助, 谢谢。

1 个答案:

答案 0 :(得分:0)

实际上tracks_response['items']会返回一个数组。仔细查看响应。您必须按顺序迭代项目列表以获得“跟踪”#39;宾语。如果您需要从艺术家列表中打印每个艺术家的名称,则代码将为

for item in tracks_response['items']:
    for artist in item['track']['album']['artists']:
         print(artist['name'])