【问题标题】:How to get the count on likes and comments for a list of twitter handle如何获得推特句柄列表的点赞数和评论数
【发布时间】:2018-08-13 12:37:28
【问题描述】:

我正在使用 R 并尝试提取总喜欢数和 cmets 以获取 twitter 句柄列表。我完全一无所知,任何帮助将不胜感激。我已经设置了 API 并安装了必要的库。

任何线索将不胜感激,谢谢

install.packages("twitteR")
library(twitteR)
install.packages("rtweet")
library(rtweet)
install.packages("tidytext")
library(tidytext)
install.packages("httr")
library(httr)
install.packages("RCurl")
library(RCurl)
install.packages("plyr")
library(plyr)
install.packages("RJSONIO")
library(RJSONIO)
install.packages("stringr")
library(stringr)
install.packages("ROAuth")
library(ROAuth)
#Setting up twitter API

consumer_key <- "key"
consumer_secret <- "key"
access_token <- "key"
access_secret <- "key"
options(httr_oauth_cache=T) 
setup_twitter_oauth(consumer_key,
                consumer_secret,
                access_token,
                access_secret)

#reading csv and extracting the followers and tweets count


users <- read.csv("Twitter.csv", skip = 1)
users1 <- lookupUsers(users[1:50,1]) 

【问题讨论】:

  • 请添加更多详细信息。您正在尝试使用哪些 API 和库,到目前为止您已经开始编写什么等等。请让请求更简洁地说明您要完成的工作。现在的要求是有人为你写。
  • 欢迎来到 SO!请阅读How to Ask 并提供minimal reproducible example

标签: r twitter count social-likes


【解决方案1】:

在 R 中,使用 rtweet,以下是您将获得的有关用户的信息

library(rtweet)

x <- rtweet::lookup_users("jdatap")
names(x)
 [1] "user_id"                 "status_id"               "created_at"             
 [4] "screen_name"             "text"                    "source"                 
 [7] "display_text_width"      "reply_to_status_id"      "reply_to_user_id"       
[10] "reply_to_screen_name"    "is_quote"                "is_retweet"             
[13] "favorite_count"          "retweet_count"           "hashtags"               
[16] "symbols"                 "urls_url"                "urls_t.co"              
[19] "urls_expanded_url"       "media_url"               "media_t.co"             
[22] "media_expanded_url"      "media_type"              "ext_media_url"          
[25] "ext_media_t.co"          "ext_media_expanded_url"  "ext_media_type"         
[28] "mentions_user_id"        "mentions_screen_name"    "lang"                   
[31] "quoted_status_id"        "quoted_text"             "quoted_created_at"      
[34] "quoted_source"           "quoted_favorite_count"   "quoted_retweet_count"   
[37] "quoted_user_id"          "quoted_screen_name"      "quoted_name"            
[40] "quoted_followers_count"  "quoted_friends_count"    "quoted_statuses_count"  
[43] "quoted_location"         "quoted_description"      "quoted_verified"        
[46] "retweet_status_id"       "retweet_text"            "retweet_created_at"     
[49] "retweet_source"          "retweet_favorite_count"  "retweet_retweet_count"  
[52] "retweet_user_id"         "retweet_screen_name"     "retweet_name"           
[55] "retweet_followers_count" "retweet_friends_count"   "retweet_statuses_count" 
[58] "retweet_location"        "retweet_description"     "retweet_verified"       
[61] "place_url"               "place_name"              "place_full_name"        
[64] "place_type"              "country"                 "country_code"           
[67] "geo_coords"              "coords_coords"           "bbox_coords"            
[70] "status_url"              "name"                    "location"               
[73] "description"             "url"                     "protected"              
[76] "followers_count"         "friends_count"           "listed_count"           
[79] "statuses_count"          "favourites_count"        "account_created_at"     
[82] "verified"                "profile_url"             "profile_expanded_url"   
[85] "account_lang"            "profile_banner_url"      "profile_background_url" 
[88] "profile_image_url"

编辑

基于您更新的代码。下面是一个可重现的例子。

users <- data.frame(
    name = c("jdatap", "dataandme"),
    stringsAsFactors = FALSE
)

rtweet::lookup_users 仅适用于 character 类的对象

users <- data.frame(
    name = c("jdatap", "dataandme")
)

class(users$name)
[1] "factor"

rtweet::lookup_users(users = users$name)
data frame with 0 columns and 0 rows

然而,character 向量一切顺利。

users <- data.frame(
    name = c("jdatap", "dataandme"),
    stringsAsFactors = FALSE
)

rtweet::lookup_users 仅适用于 character 类的对象

users <- data.frame(
    name = c("jdatap", "dataandme"),
    stringsAsFactors = FALSE
)

class(users$name)
[1] "character"

# works just not showing huge output here
users <- rtweet::lookup_users(users = users$name)

因此,当您使用 read.csv 读取 csv 时,设置 stringsAsFactors = FALSE 就像这样。

users <- read.csv("Twitter.csv", skip = 1, stringsAsFactors = FALSE)

请注意,以上是基于rtweet 包,我强烈建议您不要使用twitteR,如twitteR github page 所述。

这是 twitteR 相对悠闲弃用期的开始,转而使用 rtweet。请开始寻找切换到该软件包。如果您有任何问题,请联系我自己或@mkearney

【讨论】:

  • 谢谢!它给了我一个错误“readRDS(x) 中的错误:从连接读取错误”。你知道是什么问题吗?
  • rtweet::lookup_users 上的这个错误是从哪里得到的?查看 rtweet [Obtaining and using access tokens](rtweet.info/articles/auth.html),其中解释了如何获取令牌。然后使用rtweet::lookup_users("jdatap", token = your_token),其中your_tokencreate_token返回的对象(即:your_token &lt;- create_token()
  • 我成功创建了一个令牌,但是当我尝试使用 retweet_location 这样的标签之一时,它说功能不可用
  • 是的,这有帮助。谢谢
  • 当我使用您的编辑时,它显示错误:“lookup_user”不是从“命名空间:rtweet”导出的对象。你知道为什么会这样吗?我已经安装了 rtweet 和所有其他必要的包
猜你喜欢
  • 2021-04-04
  • 1970-01-01
  • 2018-01-13
  • 1970-01-01
  • 2018-08-02
  • 1970-01-01
  • 1970-01-01
  • 2015-06-16
  • 2019-01-25
相关资源
最近更新 更多