使用 Python 中的 Spotify API 从播放列表中获取曲目 ID

Getting the track's ID from a playlist with Spotify's API in Python

我正在尝试使用 Spotify 的 API 从播放列表中获取所有曲目的 ID

我想我真的不知道如何在 Python 上使用 json,因为这是我第一次使用 API 的

这是我请求随机播放列表时得到的json:

{ 
    "items":[ 
       { 
          "added_at":"2020-02-20T19:08:11Z",
          "added_by":{ 
          },
          "is_local":False,
          "primary_color":"None",
          "track":{ 
             "album":{ 
                "album_type":"single",
                "artists":[ 
                   { 
                      "external_urls":{ 
                         "spotify":"https://open.spotify.com/artist/1k5UEOU4igPC0NoHjEekha"
                      },
                      "href":"https://api.spotify.com/v1/artists/1k5UEOU4igPC0NoHjEekha",
                      "id":"1k5UEOU4igPC0NoHjEekha",
                      "name":"Milkoi",
                      "type":"artist",
                      "uri":"spotify:artist:1k5UEOU4igPC0NoHjEekha"
                   },
                   { 
                      "external_urls":{ 
                         "spotify":"https://open.spotify.com/artist/3U2oWd07HPgl60o8RBMG4P"
                      },
                      "href":"https://api.spotify.com/v1/artists/3U2oWd07HPgl60o8RBMG4P",
                      "id":"3U2oWd07HPgl60o8RBMG4P",
                      "name":"Miraie",
                      "type":"artist",
                      "uri":"spotify:artist:3U2oWd07HPgl60o8RBMG4P"
                   }
                ],
                "available_markets":[  ],
                "external_urls":{ 
                   "spotify":"https://open.spotify.com/album/69Y9i1D5TyQGxWdqFNRIhC"
                },
                "href":"https://api.spotify.com/v1/albums/69Y9i1D5TyQGxWdqFNRIhC",
                "id":"69Y9i1D5TyQGxWdqFNRIhC",
                "images":[ 
                   { 
                      "height":640,
                      "url":"https://i.scdn.co/image/4ea41b9dde13c6cb31fff8fe3c5ee90076370885",
                      "width":640
                   },
                   { 
                      "height":300,
                      "url":"https://i.scdn.co/image/6edf03567c0379d246c750147fd31a74574e4e27",
                      "width":300
                   },
                   { 
                      "height":64,
                      "url":"https://i.scdn.co/image/fbbc5cca3adbaf433f43917012939c3e2c35c5eb",
                      "width":64
                   }
                ],
                "name":"ミユキ",
                "release_date":"2018-09-30",
                "release_date_precision":"day",
                "total_tracks":1,
                "type":"album",
                "uri":"spotify:album:69Y9i1D5TyQGxWdqFNRIhC"
             },
             "artists":[ 
                { 
                   "external_urls":{ 
                      "spotify":"https://open.spotify.com/artist/1k5UEOU4igPC0NoHjEekha"
                   },
                   "href":"https://api.spotify.com/v1/artists/1k5UEOU4igPC0NoHjEekha",
                   "id":"1k5UEOU4igPC0NoHjEekha",
                   "name":"Milkoi",
                   "type":"artist",
                   "uri":"spotify:artist:1k5UEOU4igPC0NoHjEekha"
                },
                { 
                   "external_urls":{ 
                      "spotify":"https://open.spotify.com/artist/3U2oWd07HPgl60o8RBMG4P"
                   },
                   "href":"https://api.spotify.com/v1/artists/3U2oWd07HPgl60o8RBMG4P",
                   "id":"3U2oWd07HPgl60o8RBMG4P",
                   "name":"Miraie",
                   "type":"artist",
                   "uri":"spotify:artist:3U2oWd07HPgl60o8RBMG4P"
                }
             ],
             "available_markets":[  ],
             "disc_number":1,
             "duration_ms":211090,
             "episode":False,
             "explicit":False,
             "external_ids":{ 
                "isrc":"QM42K1817396"
             },
             "external_urls":{ 
                "spotify":"https://open.spotify.com/track/77xwKl9jpVLO6VmNlwGwtm"
             },
             "href":"https://api.spotify.com/v1/tracks/77xwKl9jpVLO6VmNlwGwtm",
             "id":"77xwKl9jpVLO6VmNlwGwtm",
             "is_local":False,
             "name":"ミユキ",
             "popularity":43,
             "preview_url":"https://p.scdn.co/mp3-preview/45e0b6cf4f358f5fbf6bebc1f019e67a780fa3f8?cid=2cd60e0da58b47518a61cec560d21ccd",
             "track":True,
             "track_number":1,
             "type":"track",
             "uri":"spotify:track:77xwKl9jpVLO6VmNlwGwtm"
          },
          "video_thumbnail":{ 
             "url":"None"
          }
       },
       { 
          "added_at":"2020-02-20T19:08:21Z",
          "added_by":{ 
             },
             "href":"https://api.spotify.com/v1/users/akqpr9b7ycor7uw08afmc3hx4",
             "id":"akqpr9b7ycor7uw08afmc3hx4",
             "type":"user",
             "uri":"spotify:user:akqpr9b7ycor7uw08afmc3hx4"
          },
          "is_local":False,
          "primary_color":"None",
          "track":{ 
             "album":{ 
                "album_type":"album",
                "artists":[ 
                   { 
                      "external_urls":{ 
                         "spotify":"https://open.spotify.com/artist/24HASvYQG1OvEFRWVWmOfx"
                      },
                      "href":"https://api.spotify.com/v1/artists/24HASvYQG1OvEFRWVWmOfx",
                      "id":"24HASvYQG1OvEFRWVWmOfx",
                      "name":"Kano",
                      "type":"artist",
                      "uri":"spotify:artist:24HASvYQG1OvEFRWVWmOfx"
                   }
                ],
                "available_markets":[  ],
                "external_urls":{ 
                   "spotify":"https://open.spotify.com/album/72sG7hFVmyFlxg9e7PfV0K"
                },
                "href":"https://api.spotify.com/v1/albums/72sG7hFVmyFlxg9e7PfV0K",
                "id":"72sG7hFVmyFlxg9e7PfV0K",
                "images":[ 
                   { 
                      "height":640,
                      "url":"https://i.scdn.co/image/ab67616d0000b27327dfa5f6ab057a4ec5c53235",
                      "width":640
                   },
                   { 
                      "height":300,
                      "url":"https://i.scdn.co/image/ab67616d00001e0227dfa5f6ab057a4ec5c53235",
                      "width":300
                   },
                   { 
                      "height":64,
                      "url":"https://i.scdn.co/image/ab67616d0000485127dfa5f6ab057a4ec5c53235",
                      "width":64
                   }
                ],
                "name":"rye",
                "release_date":"2018-12-19",
                "release_date_precision":"day",
                "total_tracks":14,
                "type":"album",
                "uri":"spotify:album:72sG7hFVmyFlxg9e7PfV0K"
             },
             "artists":[ 
                { 
                   "external_urls":{ 
                      "spotify":"https://open.spotify.com/artist/24HASvYQG1OvEFRWVWmOfx"
                   },
                   "href":"https://api.spotify.com/v1/artists/24HASvYQG1OvEFRWVWmOfx",
                   "id":"24HASvYQG1OvEFRWVWmOfx",
                   "name":"Kano",
                   "type":"artist",
                   "uri":"spotify:artist:24HASvYQG1OvEFRWVWmOfx"
                }
             ],
             "available_markets":[  ],
             "disc_number":2,
             "duration_ms":222249,
             "episode":False,
             "explicit":False,
             "external_ids":{ 
                "isrc":"JPTE01809900"
             },
             "external_urls":{ 
                "spotify":"https://open.spotify.com/track/6c9llTTjTcLgoHbKaJVw4f"
             },
             "href":"https://api.spotify.com/v1/tracks/6c9llTTjTcLgoHbKaJVw4f",
             "id":"6c9llTTjTcLgoHbKaJVw4f",
             "is_local":False,
             "name":"六兆年と一夜物語",
             "popularity":39,
             "preview_url":"https://p.scdn.co/mp3-preview/3421753cafdf34dc1e34bba479f048ebd613f39f?cid=2cd60e0da58b47518a61cec560d21ccd",
             "track":True,
             "track_number":3,
             "type":"track",
             "uri":"spotify:track:6c9llTTjTcLgoHbKaJVw4f"
          },
          "video_thumbnail":{ 
             "url":"None"
          },
    ],
    "limit":100,
    "next":"None",
    "offset":0,
    "previous":"None",
    "total":11
 }


每个轨道的 ID 位于 "items" -> "track" -> "id"

我正在尝试从播放列表的所有曲目中获取曲目的 ID,以便将它们放入列表中。

如果有人能帮助我,那就太好了

提前致谢

我正在使用 Python 3

我要做的是像这样将响应字符串放入响应字典中(如果您还没有的话)

resp_dict = json.load(your_response_string)

然后您可以遍历并将所有曲目 ID 附加到新列表

newList = []
for x in response_dict['items']:
    newList.append(x['track']['id'])

# To see if it worked
for item in newList:
    print(item)

让我知道这是否有意义,或者您需要进一步的解释:)