【发布时间】:2023-03-08 04:58:01
【问题描述】:
我正在使用 Spotipy 与 Spotify API 进行交互。我已经成功地从一位艺术家、其专辑和每张专辑的曲目中获取信息。我可以获得曲目 ID、名称和受欢迎程度。但是,当我尝试使用音频功能来获取价时,它不起作用。
这是我的代码:
for a in artists_array
artist_search = sp.search(a,1,0,"artist")
artist = artist_search['artists']['items'][0]
artists.append({'name': artist['name'], 'popularity': artist['popularity']})
albums = []
albums_ids = []
spotify_albums = sp.artist_albums(artist['id'], album_type='album')
for i in range(len(spotify_albums['items'])):
album_id = spotify_albums['items'][i]['id']
albums_ids.append(spotify_albums['items'][i]['id'])
album_name = spotify_albums['items'][i]['name']
albums.append({'id': album_id,'name': album_name })
albums_songs = []
albumIndex = 0;
#For each album
for id in albums_ids:
albums_songs.append([])
spotify_songs = sp.album_tracks(id)
for n in range(len(spotify_songs['items'])):
song_id = spotify_songs['items'][n]['id']
song_name = spotify_songs['items'][n]['name']
song_popularity = sp.track(song_id)['popularity']
song_valence = sp.audio_features([song_id])['valence']
albums_songs[albumIndex].append({'id': song_id, 'name': song_name, 'album_id': id, 'popularity': song_popularity, 'valence': song_valence })
问题出在:
song_valence = sp.audio_features(song_id)['valence']
抛出:
Traceback (most recent call last):
File "spotifyAPI/server.py", line 110, in <module>
song_valence = sp.audio_features(list_song_id[0])['valence']
TypeError: list indices must be integers or slices, not str
我知道我有正确的 song_id,因为它适用于受欢迎程度。如果我去掉价部分,程序就可以完美运行。
我不明白类型错误。根据spotipy,我应该给出:
audio_features(tracks=[]) 根据 Spotify ID 获取一个或多个轨道的音频功能参数:轨道 - 列表 跟踪 URI、URL 或 ID,最多:50 个 id
这就是为什么我这样说的原因:
song_valence = sp.audio_features([song_id])['valence']
但它也不适用于:
song_valence = sp.audio_features(song_id)['valence']
【问题讨论】:
-
sp.audio_features(song_id)是一个列表,所以需要给一个整数索引。 -
更具体地说,您可能想要
sp.audio_features(song_id)[0]["valence"]之类的东西。 -
谢谢!当您发布时,我也得到了相同的解决方案。
标签: python python-3.x spotify spotipy