How to get songs from spotify based on artist name search with limit

82 Views Asked by At

Are there any method in requests to search the track using the artist name?

Using Spotify library I used to search:

sp.search(q=f'artist:{search_artist}', type='artist', limit=limit)

My Code:

import requests

client_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
client_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'

auth_url = 'https://accounts.spotify.com/api/token'

data = {
    'grant_type': 'client_credentials',
    'client_id': client_id,
    'client_secret': client_secret,
}
auth_response = requests.post(auth_url, data=data)
access_token = auth_response.json().get('access_token')
print(access_token)

base_url = 'https://api.spotify.com/v1/search'

headers = {
    'Authorization': 'Bearer {}'.format(access_token)
}
artist_name = "John"
1

There are 1 best solutions below

0
On

You can query this format

https://api.spotify.com/v1/search?q={query}&limit={limit}&type=artist

More detail in here

Demo code

import requests
import json

client_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
client_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

auth_url = 'https://accounts.spotify.com/api/token'

data = {
    'grant_type': 'client_credentials',
    'client_id': client_id,
    'client_secret': client_secret,
}
auth_response = requests.post(auth_url, data=data)
access_token = auth_response.json().get('access_token')
print(access_token)


headers = {"Authorization": "Bearer " + access_token}
limit = 2
query = 'John'
result1 = requests.get(url=f'https://api.spotify.com/v1/search?q={query}&limit={limit}&type=artist', headers=headers)
result = result1.json()
print(json.dumps(result, indent=4))

Result

{
    "artists": {
        "href": "https://api.spotify.com/v1/search?query=John&type=artist&offset=0&limit=2",
        "items": [
            {
                "external_urls": {
                    "spotify": "https://open.spotify.com/artist/0hEurMDQu99nJRq8pTxO14"
                },
                "followers": {
                    "href": null,
                    "total": 5990863
                },
                "genres": [
                    "neo mellow",
                    "singer-songwriter"
                ],
                "href": "https://api.spotify.com/v1/artists/0hEurMDQu99nJRq8pTxO14",
                "id": "0hEurMDQu99nJRq8pTxO14",
                "images": [
                    {
                        "height": 640,
                        "url": "https://i.scdn.co/image/ab6761610000e5ebe926dd683e1700a6d65bd835",
                        "width": 640
                    },
                    {
                        "height": 320,
                        "url": "https://i.scdn.co/image/ab67616100005174e926dd683e1700a6d65bd835",
                        "width": 320
                    },
                    {
                        "height": 160,
                        "url": "https://i.scdn.co/image/ab6761610000f178e926dd683e1700a6d65bd835",
                        "width": 160
                    }
                ],
                "name": "John Mayer",
                "popularity": 75,
                "type": "artist",
                "uri": "spotify:artist:0hEurMDQu99nJRq8pTxO14"
            },
            {
                "external_urls": {
                    "spotify": "https://open.spotify.com/artist/3dRfiJ2650SZu6GbydcHNb"
                },
                "followers": {
                    "href": null,
                    "total": 1319728
                },
                "genres": [
                    "orchestral soundtrack",
                    "soundtrack"
                ],
                "href": "https://api.spotify.com/v1/artists/3dRfiJ2650SZu6GbydcHNb",
                "id": "3dRfiJ2650SZu6GbydcHNb",
                "images": [
                    {
                        "height": 640,
                        "url": "https://i.scdn.co/image/ab6761610000e5eb86b13e4d2e65ebf694384ef4",
                        "width": 640
                    },
                    {
                        "height": 320,
                        "url": "https://i.scdn.co/image/ab6761610000517486b13e4d2e65ebf694384ef4",
                        "width": 320
                    },
                    {
                        "height": 160,
                        "url": "https://i.scdn.co/image/ab6761610000f17886b13e4d2e65ebf694384ef4",
                        "width": 160
                    }
                ],
                "name": "John Williams",
                "popularity": 76,
                "type": "artist",
                "uri": "spotify:artist:3dRfiJ2650SZu6GbydcHNb"
            }
        ],
        "limit": 2,
        "next": "https://api.spotify.com/v1/search?query=John&type=artist&offset=2&limit=2",
        "offset": 0,
        "previous": null,
        "total": 4
    }
}