Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct python syntax to search by artist in Spotify's Search API?

Tags:

python

spotify

What is the correct python syntax for searching by artist with Spotify's API? Maybe I'm missing something obvious (been staring at this for way too long)

Per documentation, header 'authorization' & param 'q' and 'type' are required.
https://developer.spotify.com/web-api/search-item/

What I've tried:

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'

Then I thought, maybe the parameters has to be sent as a list?:

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

But:

ERROR: TypeError: list() takes at most 1 argument (2 given)
like image 879
tbd_ Avatar asked Jan 01 '26 15:01

tbd_


2 Answers

A list is a list, not a hybrid of a map and a list, such as in PHP. The list() builtin accepts either 0 or 1 positional argument, which should be an iterable. I'd highly recommend you go through the official tutorial.

You're probably using the python-requests library. In order to pass query parameters such as the q parameter, 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' })

Note that the headers argument must be in its plural form, not "header".

Finally, you might be interested in spotipy, a simple client for the Spotify web API.

like image 154
Ilja Everilä Avatar answered Jan 03 '26 04:01

Ilja Everilä


@Ilja's and @alfasin's answers provide good guidance but don't seem to work anymore as is.

You have to change to the headers params to authorization and add the string Bearer.

This works for me:

artist_info = requests.get('https://api.spotify.com/v1/search',
    headers={ 'authorization': "Bearer " + token}, 
    params={ 'q': artist_name, 'type': 'artist' })
like image 40
petezurich Avatar answered Jan 03 '26 04:01

petezurich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!