Last active
December 17, 2015 01:19
-
-
Save IronistM/5527588 to your computer and use it in GitHub Desktop.
source : Text Data Mining with Twitter and R (Posted on April 8, 2011) | http://heuristically.wordpress.com/2011/04/08/text-data-mining-twitter-r/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
### Read tweets from Twitter using ATOM (XML) format | |
### | |
# loading the package is required once each session | |
require(XML) | |
# installation is required only required once and is rememberd across sessions. Uncomment the next line if you lack XML package | |
# install.packages('XML') | |
# initialize a storage variable for Twitter tweets along a query one | |
mydata.vectors <- character(0) | |
query <- 'Google Analytics + R = FUN!' | |
# paginate to get more tweets | |
for (page in c(1:15)) | |
{ | |
# search parameter | |
twitter_q <- URLencode(query) | |
# construct a URL | |
twitter_url = paste('http://search.twitter.com/search.atom?q=',twitter_q,'&rpp=100&page=', page, sep='') | |
# fetch remote URL and parse | |
mydata.xml <- xmlParseDoc(twitter_url, asText=F) | |
# extract the titles | |
mydata.vector <- xpathSApply(mydata.xml, '//s:entry/s:title', xmlValue, namespaces =c('s'='http://www.w3.org/2005/Atom')) | |
# aggregate new tweets with previous tweets | |
mydata.vectors <- c(mydata.vector, mydata.vectors) | |
} | |
# how many tweets did we get? | |
cat("There are", length(mydata.vectors), "tweets regarding the" , query ,"post [", date(), "]") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment