r语言 - 使用 rtweet 获取超过Twitter API 允许的好友数量



我编写了以下脚本,该脚本使用rtweet每 15 分钟批量获取 75,000 个 Twitter 用户的朋友(在本例中为"barackobama"((每个 API 调用 5000 个朋友 x 15 个 API 调用(。但是,脚本运行完毕后,我发现好友 id 在固定间隔后重复。例如,第 1 行、第 280001 行和第 560001 行具有相同的 ID,第 2 行、280002 行和 560002 具有相同的 ID,依此类推。我想知道我是否错误地理解了 API 中的next_cursor

u = "barackobama"
n_friends = lookup_users(u)$friends_count
curr_page = -1
fetched_friends = 0
i = 0
all_friends = NULL
while(fetched_friends < n_friends)  {
if(rate_limit("get_friends")$remaining == 0) {
print(paste0("API limit reached. Reseting  at ", rate_limit("get_friends")$reset_at))
Sys.sleep(as.numeric((rate_limit("get_friends")$reset + 0.1) * 60))
}
curr_friends = get_friends(u, n = 5000, retryonratelimit = TRUE, page = curr_page)
i = i + 1
all_friends = rbind(all_friends, curr_friends)
fetched_friends = nrow(all_friends)
print(paste0(i, ". ", fetched_friends, " out of ", n_friends, " fetched."))
curr_page = next_cursor(curr_friends)
}

任何帮助将不胜感激。

你没有做错任何事。从文档中:

此排序可能会受到未经宣布的更改和最终一致性问题的影响

对于非常大的列表,API 根本不会返回您想要的所有信息。

最新更新