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

问题描述 投票:0回答:1

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

[我认为我只是真的不知道如何在Python上使用json,因为这是我第一次使用API​​'s

这是我请求随机播放列表时得到的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位于“项目”->“轨道”->“ id”

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

[如果有人可以帮助我,那将很酷

提前感谢

我正在使用Python 3

python json api spotify
1个回答
0
投票

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

resp_dict = json.load(your_response_string)

然后您可以循环浏览并将所有曲目ID附加到新列表中

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

print(newList)

语法可能有点差,但是请告诉我是否可行。

© www.soinside.com 2019 - 2024. All rights reserved.