1

嘿嘿,我需要帮助从使用 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',...

这与上面的内容相同,但从“项目”键开始......看来字典的其余部分是其中包含键值对的列表,我将如何从它们中获取我想要的数据?

任何帮助表示赞赏,谢谢。

4

1 回答 1

0

实际上tracks_response['items']返回一个数组。仔细查看响应。您必须迭代项目列表才能获得“跟踪”对象。如果您需要从艺术家列表中打印每个艺术家的姓名,代码将是,

for item in tracks_response['items']:
    for artist in item['track']['album']['artists']:
         print(artist['name'])   
于 2018-06-02T18:09:31.953 回答