如何使用Spotipy获取此代码以返回播放列表中的所有歌曲?
我正在尝试使用Spotipy中的播放列表中的歌曲中获得Spotify Audiofeatures。但是,我只在播放列表中获取最后一首歌的数据。我的代码看起来像这样:
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import time
import numpy as np
import pandas
client_id = 'ID'
client_secret = "Secret"
#Authentication - without user
client_credentials_manager = SpotifyClientCredentials(client_id=client_id,
client_secret=client_secret)
sp = spotipy.Spotify(client_credentials_manager = client_credentials_manager)
playlist_link = "https://open.spotify.com/playlist/3VJlwgnV4IaxGK8uIEZMjV?
si=ca8c506dd5d04663"
playlist_URI = playlist_link.split("/")[-1].split("?")[0]
track_uris = [x["track"]["uri"] for x in sp.playlist_tracks(playlist_URI)["items"]]
for track in sp.playlist_tracks(playlist_URI)["items"]:
#URI
track_uri = track["track"]["uri"]
#Track name
track_name = track["track"]["name"]
#Main Artist
artist_uri = track["track"]["artists"][0]["uri"]
artist_info = sp.artist(artist_uri)
#Name, popularity, genre
artist_name = track["track"]["artists"][0]["name"]
artist_pop = artist_info["popularity"]
artist_genres = artist_info["genres"]
#Album
album = track["track"]["album"]["name"]
#Popularity of the track
track_pop = track["track"]["popularity"]
result = track_name, sp.audio_features(track_uri)
result
我已经填写了ID,但我在此处将其删除以进行隐私。
I am trying to get the spotify audiofeatures from the songs in a playlist using spotipy. However, I only get the data of the last song in the playlist. My code looks like this:
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import time
import numpy as np
import pandas
client_id = 'ID'
client_secret = "Secret"
#Authentication - without user
client_credentials_manager = SpotifyClientCredentials(client_id=client_id,
client_secret=client_secret)
sp = spotipy.Spotify(client_credentials_manager = client_credentials_manager)
playlist_link = "https://open.spotify.com/playlist/3VJlwgnV4IaxGK8uIEZMjV?
si=ca8c506dd5d04663"
playlist_URI = playlist_link.split("/")[-1].split("?")[0]
track_uris = [x["track"]["uri"] for x in sp.playlist_tracks(playlist_URI)["items"]]
for track in sp.playlist_tracks(playlist_URI)["items"]:
#URI
track_uri = track["track"]["uri"]
#Track name
track_name = track["track"]["name"]
#Main Artist
artist_uri = track["track"]["artists"][0]["uri"]
artist_info = sp.artist(artist_uri)
#Name, popularity, genre
artist_name = track["track"]["artists"][0]["name"]
artist_pop = artist_info["popularity"]
artist_genres = artist_info["genres"]
#Album
album = track["track"]["album"]["name"]
#Popularity of the track
track_pop = track["track"]["popularity"]
result = track_name, sp.audio_features(track_uri)
result
I have filled in the ID's but I removed them here for privacy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题没有给我运行代码所需的信息,所以我盲目地回答了这个问题。
您不会在 for 循环结束时返回,并且您可能希望将结果添加到结构中并返回该结果。
下面已经(猜到)你想要什么了。其他评论:
编辑
此消息作为通知弹出,因为有人认为它有帮助。这是我将如何做到这一点的更新版本。
Your question didn't give me the necessary information to run the code, so I have answered this blind.
You are not returning at the end of the for loop and you probably want to add the results to a structure and return that.
The below has (guessed) what you want. Few other comments:
Edit
This popped up as a notification as someone thought it was helpful. Here is a more updated version of how I would do it.