'Getting a 404 error at random times when trying run a search endpoint on Spotify's API using Spotipy?

Just for simplicity, say I want to search Beyonce 1000 times

artist_name_list = []
for i in np.arange (0, 1000, 1):
  searchResults = sp.search("beyonce",1,0,"artist")
  artist = searchResults['artists']['items'][0]
  artist_name = artist['name']
  artist_name_list.append(artist_name)

When I run this, I get a 404 error at seemingly random points, which I can tell because the len(artist_name_list) differs every time I run it.

HTTP Error for GET to https://api.spotify.com/v1/search with Params: {'q': 'beyonce', 'limit': 1, 'offset': 0, 'type': 'artist', 'market': None} returned 404 due to None

Could someone help understand what's going on? It seemed to run earlier in the day.

Thank you!



Solution 1:[1]

The only thing I can think of is that you're hitting some limitation on the API usage, maybe try inserting a pause between API requests, e.g. using time.sleep:

import time
time.sleep(5) # waits for 5 seconds

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rob_med