使用 rtweet 获得超过 Twitter API 允许的好友数量

Getting more than the number of friends allowed by Twitter API using rtweet

我编写了以下脚本,以 75,000 个批次(5000 个朋友每次 API 调用 x 15 API 调用)获取 Twitter 用户(本例中的 "barackobama")的朋友每 15 分钟使用 rtweet。但是,脚本完成后 运行,我发现朋友 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)
}

任何帮助将不胜感激。

你没有做错任何事。来自 the documentation:

this ordering is subject to unannounced change and eventual consistency issues

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