2

我正在使用 R 并尝试提取 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]) 
4

1 回答 1

0

在 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.csvset读取 csv 时。stringsAsFactors = FALSE

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

注意以上是基于rtweet包的,我强烈建议你从 using 切换twitteR,如twitteR github 页面所述。

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

于 2018-08-13T12:45:37.200 回答