[GH-ISSUE #197] artist search error #100

Closed
opened 2026-02-27 23:20:50 +03:00 by kerem · 2 comments
Owner

Originally created by @lohriialo on GitHub (Jun 9, 2017).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/197

I'm trying to search for artist like so but it's throwing KeyError:

query = 'year:2017'
results = sp.search(query, limit=50, type='artist')
artists = results['artists']
while artists:
for i, artist in enumerate(artists['items']):
print("%4d %s %s" % (i + 1 + artists['offset'], artist['id'], artist['name']))
if artists['next']:
artists = sp.next(artists)
else:
artists = None

The error I get is:

for i, artist in enumerate(artists['items']):
KeyError: 'items'

While the same type of call for "user_playlist" works fine.

For example:

playlists = sp.user_playlists('spotify')
while playlists:
for i, playlist in enumerate(playlists['items']):
print("%4d %s %s" % (i + 1 + playlists['offset'], playlist['uri'], playlist['name']))
if playlists['next']:
playlists = sp.next(playlists)
else:
playlists = None

Anyone got any idea?

Originally created by @lohriialo on GitHub (Jun 9, 2017). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/197 I'm trying to search for artist like so but it's throwing KeyError: **query = 'year:2017' results = sp.search(query, limit=50, type='artist') artists = results['artists'] while artists: for i, artist in enumerate(artists['items']): print("%4d %s %s" % (i + 1 + artists['offset'], artist['id'], artist['name'])) if artists['next']: artists = sp.next(artists) else: artists = None** The error I get is: **for i, artist in enumerate(artists['items']): KeyError: 'items'** While the same type of call for "user_playlist" works fine. For example: **playlists = sp.user_playlists('spotify') while playlists: for i, playlist in enumerate(playlists['items']): print("%4d %s %s" % (i + 1 + playlists['offset'], playlist['uri'], playlist['name'])) if playlists['next']: playlists = sp.next(playlists) else: playlists = None** Anyone got any idea?
kerem closed this issue 2026-02-27 23:20:50 +03:00
Author
Owner

@kotutuloro commented on GitHub (Jun 9, 2017):

Using sp.next() returns search results in a format similar to the original results.
So just like you had to retrieve the artists from the results in the initial search with artists = results['artists'] before you could use artists['items'], you have to do the same when you're reassigning artists.
You could use this instead and it should work:

if artists['next']:
    artists = sp.next(artists)['artists']

The structure of the response for playlists is different. The items key is immediately accessible from the results, so using playlists = sp.next(playlists) and then using playlist['items'] on the next iteration doesn't result in an error.

<!-- gh-comment-id:307457191 --> @kotutuloro commented on GitHub (Jun 9, 2017): Using `sp.next()` returns search results in a format similar to the original results. So just like you had to retrieve the artists from the results in the initial search with `artists = results['artists']` before you could use `artists['items']`, you have to do the same when you're reassigning artists. You could use this instead and it should work: ```python if artists['next']: artists = sp.next(artists)['artists'] ``` The structure of the response for playlists is different. The items key is immediately accessible from the results, so using `playlists = sp.next(playlists)` and then using `playlist['items']` on the next iteration doesn't result in an error.
Author
Owner

@lohriialo commented on GitHub (Jun 16, 2017):

Thanks, I got this working like so:

query = 'year:2017' results = sp.search(query, limit=50, type='artist') while results: artists = results['artists'] for i, artist in enumerate(artists['items']): artist_ids.append(artist['id']) if results['next']: results = sp.next(artists) else: results = None

<!-- gh-comment-id:309006307 --> @lohriialo commented on GitHub (Jun 16, 2017): Thanks, I got this working like so: `query = 'year:2017' results = sp.search(query, limit=50, type='artist') while results: artists = results['artists'] for i, artist in enumerate(artists['items']): artist_ids.append(artist['id']) if results['next']: results = sp.next(artists) else: results = None`
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#100
No description provided.