在Spotify的Search API中用艺术家搜索python语法是否正确?

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

使用Spotify API搜索艺术家的正确python语法是什么?也许我错过了一些明显的东西(一直盯着这个太长时间)

根据文档,标题'授权'和参数'q'和'类型'是必需的。 https://developer.spotify.com/web-api/search-item/

我尝试过的:

artist_name = 'Linkin%20Park'
artist_info = requests.get('https://api.spotify.com/v1/search', header = {'access_token': access_token}, q = artist_name, type = 'artist')

ERROR: TypeError: requests() got an unexpected keyword argument 'q'

然后我想,也许参数必须作为列表发送?:

artist_info = requests.get('https://api.spotify.com/v1/search', header = {'access_token': access_token}, query = list(q = artist_name, type = 'artist'))

但:

ERROR: TypeError: list() takes at most 1 argument (2 given)
python api spotify
3个回答
3
投票

列表是一个列表,而不是地图和列表的混合,例如在PHP中。 list() builtin接受0或1位置参数,这应该是可迭代的。我强烈建议你通过官方tutorial

你可能正在使用python-requests库。为了传递查询参数,例如q参数,you'd pass a dict of parameters as the params argument

artist_info = requests.get(
    'https://api.spotify.com/v1/search',
    headers={ 'access_token': access_token },
    params={ 'q': artist_name, 'type': 'artist' })

请注意标题参数must be in its plural form, not "header"

最后,您可能对spotipy感兴趣,artist_info = requests.get('https://api.spotify.com/v1/search?q={}&type={}'.format(artist_name, 'artist'), header = {'access_token': access_token}) 是Spotify Web API的简单客户端。


2
投票

@ Ilja的回答很好。或者,您可以在URL中嵌入params(因为您只有两个并且都相对较短),例如:

headers

1
投票

@ Ilja和@ alfasin的答案提供了良好的指导,但似乎不再有效。

你必须改为authorization参数到Bearer并添加字符串artist_info = requests.get('https://api.spotify.com/v1/search', headers={ 'authorization': "Bearer " + token}, params={ 'q': artist_name, 'type': 'artist' })

这对我有用:

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