R Programming Twitter Scraper



Create a Twitter Scrapper using R programming language in these very simple steps:


Follow these simple steps to start srapping:

  1. Create Twitter API Application first: https://apps.twitter.com/.

  2. Copy the code below into an R script.

  3. Install packages “twitteR” and “ROAuth” if required.

  4. Change the search terms to your liking.

    library(twitteR) library(ROAuth)

    Set API Keys

    api_key <- “XXXXXXXXXXXXXXXXXXXX” api_secret <- “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”

    access_token <- “xxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx” access_token_secret <- “xxxxxxxxxxxxxxxxxxxxxxxxxxxx”

    setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)

    Grab latest tweets

    tweets_trump = searchTwitter(‘from:realDonaldTrump’, n=50) tweets_trump2 = searchTwitter(‘@realDonaldTrump’, n = 200) tweets_trump3 = searchTwitter(‘morning+bacon’, n=100)

    Transform tweets list into a data frame

    tweets.df <- twListToDF(tweets_trump) tweets.df2 = twListToDF(tweets_trump2) tweets.df3 = twListToDF(tweets_trump3)

    write.csv(tweets.df, file = “tweets.csv”, row.names = FALSE) write.csv(tweets.df2, file = “tweets2.csv”, row.names = FALSE) write.csv(tweets.df3, file = “tweets3.csv”, row.names = FALSE)

Example Output Files here:




When dealing with Corpi and text, you may encounter an issue as I had in this post involving putting the text to lowercase. An odd error but solved!

comments powered by Disqus