我在ruby中使用Twitter gem(1.7.1)。我希望在Twitter上获得我的列表成员,但我得到最后二十名成员。我如何才能获得超过二十名成员?
listmembers = client.list_members(username, listname)
listmembers.users.each do |lmname|
p lmname.screen_name
end
答案 0 :(得分:7)
首先,传递参数“cursor = -1”。然后你获得前20个.Twitter API将返回一个名为“next_cursor”的字段 - 将其传递给光标,然后你得到下一个20。
示例(伪代码):
cursor = -1
do {
# get data, using the cursor
data = {}
cursor = data.next_cursor
} while (len(data) > 0) # Loop while we're still getting data back
的更多信息
答案 1 :(得分:1)
我使用“游标”解决了我的问题。这个解决方案帮助了我。 How to use Twitter Api To Get more than 20 list Members in a single request?
我编辑了ruby的代码。我的代码是那样的;我保留了一个整数值,
pagevalue = 0
if (listmembers.next_cursor == 0) # it means we have only one page
listmembers.users.each do |lmname|
p lmname.screen_name
end
end
else # it means we have pages more than one
while (listmembers.next_cursor != 0)
listmembers.users.each do |lmname|
p lmname.screen_name # check screen_name is nil or not
end
end
pagevalue = listmembers.next_cursor
listmembers = client.list_members(username, listname, :cursor => pagevalue)
if (listmembers.next_cursor == 0) # for last page
listmembers.users.each do |lmname|
p lmname.screen_name
end
end
end
如果你使用screen_name nil,它将无效。因此,请检查您的screen_name是否存在。