如何在使用 Spotipy 使用 Spotify API 检索曲目信息时修复 http 错误
我希望使用Spotipy库中的Spotify API检索特定轨道的音频功能。但是,我首先试图看看是否可以使用Spotipy获取一些信息。但是,当我复制Spotipy文档的文字代码时,它不起作用。它可以解决错误:获取http错误到http错误,访问 https://api.spotify.com /v1/search 带有参数:{'q':'年:2018','limit':50,'offset':1000,'type':'track':'track','market':none}返回404由于没有找到。或其他HTTP错误 这似乎很奇怪,因为我使用文档中提供的代码。我尝试了几个不同的代码,到处都有相同的错误。我必须做错事,但我不知道什么。
这是代码:
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
cid = 'ID'
secret = 'key'
client_credentials_manager = SpotifyClientCredentials(client_id=cid, client_secret=secret)
sp = spotipy.Spotify(client_credentials_manager
=
client_credentials_manager)
artist_name = []
track_name = []
popularity = []
track_id = []
for i in range(0,10000,50):
track_results = sp.search(q='year:2018', type='track', limit=50,offset=i)
for i, t in enumerate(track_results['tracks']['items']):
artist_name.append(t['artists'][0]['name'])
track_name.append(t['name'])
track_id.append(t['id'])
popularity.append(t['popularity'])
track_dataframe = pd.DataFrame({'artist_name' : artist_name, 'track_name' : track_name, 'track_id' : track_id, 'popularity' : popularity})
print(track_dataframe.shape)
track_dataframe.head()
I am looking to retrieve audio features of a specific track using the spotipy library for the Spotify API. However, I am first trying to see if I can use spotipy to get some information. But when I copy the literal code of the spotipy documentation, it does not work. It geves the error: HTTP Error for GET to HTTP Error for GET to https://api.spotify.com/v1/search with Params: {'q': 'year:2018', 'limit': 50, 'offset': 1000, 'type': 'track', 'market': None} returned 404 due to Not found. or other HTTP errors
This seems strange since I litterly use the code provided in the documentation. I have tried this with several different code and everywhere I get the same error. I must do something wrong but I haven't got a clue what.
This is the code:
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
cid = 'ID'
secret = 'key'
client_credentials_manager = SpotifyClientCredentials(client_id=cid, client_secret=secret)
sp = spotipy.Spotify(client_credentials_manager
=
client_credentials_manager)
artist_name = []
track_name = []
popularity = []
track_id = []
for i in range(0,10000,50):
track_results = sp.search(q='year:2018', type='track', limit=50,offset=i)
for i, t in enumerate(track_results['tracks']['items']):
artist_name.append(t['artists'][0]['name'])
track_name.append(t['name'])
track_id.append(t['id'])
popularity.append(t['popularity'])
track_dataframe = pd.DataFrame({'artist_name' : artist_name, 'track_name' : track_name, 'track_id' : track_id, 'popularity' : popularity})
print(track_dataframe.shape)
track_dataframe.head()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 Spotify 的 API 文档,最大数量搜索中可以返回的项目为 1000,这就是为什么 950 是仍然有结果的最大偏移量。
As per Spotify's API documentation, the maximum amount of items that can be returned in a search is 1000, that's why 950 is the largest offset that still has results.