Create a Twitter Scrapper using R programming language in these very simple steps:
[embed]https://youtu.be/1_K01qD4Exw[/embed]
Follow these simple steps to start srapping:
Create Twitter API Application first: https://apps.twitter.com/.
Copy the code below into an R script.
Install packages “twitteR” and “ROAuth” if required.
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!