简体   繁体   中英

Filter tweets for a given account or ID, but not having to account for tweets published from this account in R?

I would like to extract tweets with R for any determined account. Is that possible? I have tried with:

library(rtweet)

api_key <- "xxxxxxxxxxxxxxxxxxxxxxxxxx"
api_secret_key <- "xxxxxxxxxxxxxxxxxxxxxxxxxx"
access_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
access_secret ="xxxxxxxxxxxxxxxxxxxxxxxxxx"

token <- create_token(
  app = "example coding",
  consumer_key = api_key,
  consumer_secret = api_secret_key,
  access_token = access_token,
  access_secret = access_secret)

search_tweets("@elonmusk", n = 10, lang ='en') 

I will really appreciate any help, thank you in advance.

If I am understanding correctly, then you would like to only return tweets by the specific user and not mentions, etc. search_tweets will return any mentions of that user, but if you want to get that accounts actual tweets, then you can use get_timeline , which can return the n most recent tweets.

library(rtweet)

get_timeline("elonmusk", n = 5)

Output

# A tibble: 5 × 90
  user_id  status_id  created_at          screen_name
  <chr>    <chr>      <dttm>              <chr>      
1 44196397 152816042… 2022-05-21 23:46:52 elonmusk   
2 44196397 152815906… 2022-05-21 23:41:27 elonmusk   
3 44196397 152815878… 2022-05-21 23:40:20 elonmusk   
4 44196397 152815834… 2022-05-21 23:38:35 elonmusk   
5 44196397 152812441… 2022-05-21 21:23:47 elonmusk   
# … with 86 more variables: text <chr>,
#   source <chr>, display_text_width <dbl>,
#   reply_to_status_id <chr>,
#   reply_to_user_id <chr>,
#   reply_to_screen_name <chr>, is_quote <lgl>,
#   is_retweet <lgl>, favorite_count <int>,
#   retweet_count <int>, quote_count <int>, …

Then, you can do other filtering/subsetting to remove replies, etc., such as:

library(dplyr)

get_timeline("elonmusk", n = 5) %>% 
  filter(is.na(reply_to_status_id))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM