Spotipy:如何从播放列表中读取100多首曲目 [英] Spotipy: How to read more than 100 tracks from a playlist
问题描述
我正在尝试使用 Spotipy库用于python.
I'm trying to pull all tracks in a certain playlist using the Spotipy library for python.
user_playlist_tracks函数被限制为100个轨道,而与参数限制无关. Spotipy文档将其描述为:
The user_playlist_tracks function is limited to 100 tracks, regardless of the parameter limit. The Spotipy documentation describes it as:
user_playlist_tracks(用户,playlist_id =无,字段=无,限制= 100, 偏移量= 0,市场=无)
user_playlist_tracks(user, playlist_id=None, fields=None, limit=100, offset=0, market=None)
获取播放列表曲目的完整详细信息 由用户拥有.
Get full details of the tracks of a playlist owned by a user.
参数:
- 用户
- 用户playlist_id的ID
- 播放列表字段的ID
- 要返回限制的字段
- 返回偏移量的最大轨道数
- 返回市场的第一条轨道的指数
- ISO 3166-1 alpha-2国家/地区代码.
- user
- the id of the user playlist_id
- the id of the playlist fields
- which fields to return limit
- the maximum number of tracks to return offset
- the index of the first track to return market
- an ISO 3166-1 alpha-2 country code.
在通过Spotify进行身份验证之后,我目前正在使用类似这样的内容:
After authenticating with Spotify, I'm currently using something like this:
username = xxxx
playlist = #fromspotipy
sp_playlist = sp.user_playlist_tracks(username, playlist_id=playlist)
tracks = sp_playlist['items']
print tracks
有没有一种方法可以返回100条以上的曲目?我尝试在函数参数中设置limit = None,但是返回错误.
Is there a way to return more than 100 tracks? I've tried setting the limit=None in the function parameters, but it returns an error.
推荐答案
许多Spotipy方法返回分页结果,因此您必须滚动浏览它们以查看不仅仅是最大限制的结果.在收集播放列表的完整曲目列表时,我经常遇到这种情况,因此创建了一个自定义方法来处理此问题:
Many of the spotipy methods return paginated results, so you will have to scroll through them to view more than just the max limit. I've encountered this most often when collecting a playlist's full track listing and consequently created a custom method to handle this:
def get_playlist_tracks(username,playlist_id):
results = sp.user_playlist_tracks(username,playlist_id)
tracks = results['items']
while results['next']:
results = sp.next(results)
tracks.extend(results['items'])
return tracks
这篇关于Spotipy:如何从播放列表中读取100多首曲目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!