Spotify搜索API中按艺术家搜索的正确Python语法?

2024-04-27 00:23:28 发布

您现在位置:Python中文网/ 问答频道 /正文

用Spotify的API按艺术家搜索的正确python语法是什么?也许我错过了一些显而易见的东西(盯着这个看太久了)

根据文档,需要标题“authorization”和参数“q”和“type”。
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'

然后我想,也许参数必须以列表的形式发送?公司名称:

^{pr2}$

但是:

ERROR: TypeError: list() takes at most 1 argument (2 given)

Tags: namehttpscomtokenapisearch参数access
3条回答

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

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

list是一个列表,而不是map和list的混合体,比如在PHP中。^{} builtin接受0或1位置参数,这应该是iterable。我强烈建议你通过官方的tutorial。在

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

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

注意headers参数must be in its plural form, not "header"。在

最后,您可能对spotipy感兴趣,它是spotifywebapi的一个简单客户端。在

@Ilja和@alfasin的答案提供了很好的指导,但似乎不再起作用了。在

您必须将headers参数改为authorization,并添加字符串Bearer。在

这对我有用:

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

相关问题 更多 >