【问题标题】:Twitter: Get followers from multiple users at onceTwitter:一次获得多个用户的关注者
【发布时间】:2015-06-23 04:59:41
【问题描述】:

我正在开展一个项目,我需要找到一些社交活动的影响范围。我想知道有多少人在丹麦的一个名为 Tinderbox 的节日上接触了 cmets。 我所做的是在 Twitter 上获取状态,包括丹麦语中的“tinderbox”一词。然后我想从这些网名中提取关注者的数量。所以我的代码的第一部分由以下给出:

library("twitteR")
setup_twitter_oauth(consumer_key,consumer_secret,access_token,access_secret)
1
#get data
TB<-searchTwitter("tinderbox", lan="da", n=10000)
#put into a dataframe
df <- do.call("rbind", lapply(TB, as.data.frame))

我的想法是使用与下面示例相同的输出,即 直接从 twitter 数据中获取 followerCount。 该示例可在 stackoverflow 上找到。但我不知道该怎么做才能解决我的目的(fetching large number of followers and followees in R

library(twitteR)
user <- getUser("krestenb")
followers <- user$getFollowers()
b <- twListToDF(followers)
f_count <- as.data.frame(b$followersCount)
u_id <- as.data.frame(b$id)
u_sname <- as.data.frame(b$screenName)
u_name <- as.data.frame(b$name)
final_df <- cbind(u_id,u_name,u_sname,f_count)
sort_fc <- final_df[order(-f_count),]
colnames(sort_fc) <- c('id','name','s_name','fol_count')

我的问题是,我不能通过从 df$screenName 中提取屏幕名称列表来简单地在关注者

所以我的想法是,也许我需要对所有不同的屏幕名称进行循环。但我不知道该怎么做。

我知道我已经描绘了我想要得到的东西,以及我是如何想/认为我可以到达那里的。

由于节日将于本周末举行,因此非常感谢您的帮助。

【问题讨论】:

  • 我没有在我的本地机器上配置 OAuth,所以我不能使用你的代码。但我可以告诉你,在 R 中“避免”使用循环的典型方法是使用 apply 函数之一。您可以定义一个用户向量,然后使用 apply() 对其进行迭代。
  • 感谢您的快速回复。我仍然是 R 的新手——最近才开始将它用于其他东西,而不仅仅是制作统计模型。因此,如果您能在上面给出的代码中输入一些关于如何使用应用函数的示例代码,我将不胜感激。

标签: r twitter


【解决方案1】:

以下是一些示例代码,基于您在原始问题中的内容,它将汇总一组用户的 Twitter 结果:

# create a data frame with 4 columns and no rows initially
df_result <- data.frame(t(rep(NA, 4)))
names(df_result) <- c('id', 'name', 's_name', 'fol_count')
df_result <- df_result[0:0,]

# you can replace this vector with whatever set of Twitter users you want
users <- c("krestenb", "tjb25587")                    # tjb25587 (me) has no followers

# iterate over the vector of users and aggregate each user's results
sapply(users, function(x) {
                  user <- getUser(x)
                  followers <- user$getFollowers()
                  if (length(followers) > 0) {        # ignore users with no followers
                      b <- twListToDF(followers)
                      f_count <- as.data.frame(b$followersCount)
                      u_id <- as.data.frame(b$id)
                      u_sname <- as.data.frame(b$screenName)
                      u_name <- as.data.frame(b$name)
                      final_df <- cbind(u_id,u_name,u_sname,f_count)
                      sort_fc <- final_df[order(-f_count),]
                      colnames(sort_fc) <- c('id','name','s_name','fol_count')
                      df_result <<- rbind(df_result, sort_fc)
                  }
              })

要点

df_result 数据帧上执行rbind 时,我使用了全局赋值运算符&lt;&lt;-,以便它“粘”在循环之外。正如我在原始答案中提到的,您可以使用 sapply 函数来迭代用户向量。在循环内部,结果被聚合。

我用一个包含有和没有关注者的 Twitter 用户的向量进行了测试,结果有效。

【讨论】:

  • 感谢您的回复。我相信你对这种方法是正确的。但我无法弄清楚以下错误。如果我运行 twitter.users
  • 加上以下错误:can_access_other_account(user_id) 尝试应用非函数时出错另外:警告消息:在 twInterfaceObj$doAPICall("account/verify_credentials", ...) 中:遇到速率限制& 达到重试限制 - 返回部分结果
  • 如果不使用 OAuth 配置我的机器,这是我能提供的最大帮助。
  • 感谢您的努力。我会努力让它发挥作用。如果您需要,我可以将 OAuth 发送到您的电子邮箱。这是一个测试推特帐户。无论如何,我非常感谢您已经花费的时间和精力:-)。
  • @SanderEhmsen 我更新了我的答案。该代码在我的本地 R 设置上运行良好。如果对您有帮助,请将答案标记为正确。如果您有任何其他问题,也请告诉我。
猜你喜欢
  • 2018-10-17
  • 2012-07-20
  • 2013-08-28
  • 2011-08-23
  • 2011-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多