2016-11-15 60 views
1

在的SoundCloud API指南(https://developers.soundcloud.com/docs/api/guide#pagination) 阅读超过100件的数据给出的示例的SoundCloud API的Python的问题如下:带有链接的分区

# get first 100 tracks 
tracks = client.get('/tracks', order='created_at', limit=page_size) 
for track in tracks: 
    print track.title 

# start paging through results, 100 at a time 
tracks = client.get('/tracks', order='created_at', limit=page_size, 
        linked_partitioning=1) 
for track in tracks: 
    print track.title 

我敢肯定这是错误的我发现'tracks.collection'需要引用而不仅仅是'track'。基于GitHub的蟒蛇的SoundCloud API维基应该更像这样:

tracks = client.get('/tracks', order='created_at',limit=10,linked_partitioning=1) 
while tracks.collection != None: 
for track in tracks.collection: 
    print(track.playback_count) 
tracks = tracks.GetNextPartition() 

这里,我从最后一行除去缩进(我认为这是一个维基的错误是在for循环,这使得对我没有意义)。这适用于第一个循环。但是,这不适用于连续页面,因为找不到“GetNextPartition()”函数。我试过最后一行:

tracks = tracks.collection.GetNextPartition() 

...但没有成功。

也许我正在混合版本?但我试图在从这里下载版本后使用Python 3.4运行:https://github.com/soundcloud/soundcloud-python

任何帮助非常感谢!

回答

2

对于任何关心的人,我在SoundCloud开发者论坛上找到了这个解决方案。它从原始情况(搜索曲目)稍微修改以列出我自己的追随者。诀窍是反复调用client.get函数,并将先前返回的“users.next_href”作为指向下一页结果的请求。万岁!

pgsize=200 
c=1 
me = client.get('/me') 
#first call to get a page of followers 
users = client.get('/users/%d/followers' % me.id, limit=pgsize, order='id', 
        linked_partitioning=1) 
for user in users.collection: 
    print(c,user.username) 
    c=c+1 
#linked_partitioning means .next_href exists 
while users.next_href != None: 
#pass the contents of users.next_href that contains 'cursor=' to 
#locate next page of results 
users = client.get(users.next_href, limit=pgsize, order='id', 
        linked_partitioning=1) 
for user in users.collection: 
    print(c,user.username) 
    c=c+1