-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathR_scrapper.R
90 lines (59 loc) · 1.92 KB
/
R_scrapper.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#PART 8.1
#Call for installation of required libraries
#install.packages("lubridate")
#install.packages("scales")
#install.packages("broom")
#install.packages("ggplot2")
#install.packages("stringr")
#install.packages("twitteR")
#install.packages("purrr")
#install.packages("tidytext")
#install.packages("dplyr")
#install.packages("tidyr")
#install.packages("rtweet")
#Instantiate the libraries
library(dplyr)
library(tidyr)
library(lubridate)
library(scales)
library(broom)
library(ggplot2)
library(stringr)
library(twitteR)
library(purrr)
library(tidytext)
library(rtweet)
# 2) Get access to Twitter#
#Generate the Access Tokens from Twitter Developer Accounts
#CALL FOR THE TOKENs
accessToken ="921221184905084929-CNar8k2f5HN0sFiDRNB4hyy3litixUE"
accessSecret = "WE8NJsRKkHCp3sB03sRtsHaCALDn64pracvBf5KjDTSQ5"
consumerKey = "K7kLU6QVaIzPOs1FLehuaXL9a"
consumerSecret = "uAQiU2kgfWkF9cQ62QVNiJ74oO1vs6j7CIKrAbj1MRHnJmr8pP"
token <- create_token(
app = "ScrapperRJ",
consumer_key = consumerKey,
consumer_secret = consumerSecret,
access_token = accessToken,
access_secret = accessSecret)
options(httr_oauth_cache=TRUE)
setup_twitter_oauth(consumer_key = consumerKey, consumer_secret = consumerSecret,
access_token = accessToken, access_secret = accessSecret)
#3.1) Scrape a user's tweets
#Name Of UserNAme=Former President Barack Obama
#Timeline Name :@potus44
#Section 8.1
#tweets <- search_tweets(q = "POTUS44", n = 1000)
tweets<- search_tweets(q = "@potus44", n = 1000)
tweets
#Display the tweet Data
View(tweets)
# Saving on object in RData format
save(tweets, file = "data.RData")
# To load the data again
load("data.RData")
#Data Cleaning : Sentiment Orientation
tweets <- tweets %>% filter(sentiment!="Neutral")
# we are going to need the target variable to be categorical
tweets <- tweets %>% mutate(sentiment=factor(sentiment))
tweets$text <- gsub("[^[:alnum:][:blank:]?&/\\-]", "", tweets$text)