Skip to content

Instantly share code, notes, and snippets.

@IronistM
Last active December 17, 2015 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IronistM/5527588 to your computer and use it in GitHub Desktop.
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/
###
### 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