Using Twitter and Facebook API, we could extract information from Twitter easily, this project is to use R programming to extract the information from Twitter and Facebook – especially from Public Profile.
Twitter Scraper
- Sign up for Twitter API at https://apps.twitter.com/ and create an app
- Jot down ‘Key & Access Tokens’ Information
- Using the ROAuth / twitteR to obtain the information
library(ROAuth)
library(twitteR)
my_oauth consumer_key = “xxx”,
consumer_secret =”xxx”,
access_token = “xxx”,
access_secret = “xxx”)
#Specify KW and Search
tweets_search = searchTwitter(“Sherlock”, n=100)
#Convert to DF
tweets_df = twListToDF(tweets_search)write.csv(tweets_df,”test.csv”)
## Ref: https://cran.r-project.org/web/packages/twitteR/twitteR.pdf
Facebook Scraper
- Sign up for Facebook API at https://developers.facebook.com/ and create an app
- Set website URL for the app (in basic setting) to ‘http://localhost:1410/’
- Jot down ‘App ID’ and ‘Secret’
- Using the Rfacebook to obtain the information
library(Rfacebook)
# OAUTH
# Create Facebook App > Add Platform = Web > URL = http://localhost:1410/
fb_oauth# Use page name or get id from http://findmyfbid.com/
facebook_name = “singaporeair”pagedat = getPage(facebook_name, token=fb_oauth, feed=T, n=200, since = “2016-12-01″)
write.csv(pagedat,”PagePost.csv”)
# get comments in specific post
postment = getPost(pagedat$id[1], token = fb_oauth)$comments# Ref: https://cran.r-project.org/web/packages/Rfacebook/Rfacebook.pdf
If you would like to establish projects similar to above, please feel free to contact me.
p.kanokkorn@gmail.com
Kanokkorn Prasongthanakit
The rest of this blog (except the showcases above) was written in Thai as I intended this to be a personal journal.