【发布时间】:2020-02-14 22:56:40
【问题描述】:
我编写了以下脚本,该脚本每 15 分钟使用 rtweet 分批获取 75,000 个(每个 API 调用 5000 个朋友 x 15 个 API 调用)的 Twitter 用户(在本例中为“barackobama”)的朋友。但是,在脚本运行完成后,我发现朋友 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)
}
任何帮助将不胜感激。
【问题讨论】: