Skip to content

Instantly share code, notes, and snippets.

@mamonu
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save mamonu/01eb2c902ac236234c2a to your computer and use it in GitHub Desktop.

Select an option

Save mamonu/01eb2c902ac236234c2a to your computer and use it in GitHub Desktop.
get Stack Overflow programming language use data
# Get StackOverflow data
get.stack<-function(tok) {
# Must check for XML install, thanks onertipaday!
if (!require(XML)) install.packages('XML')
library(XML)
# Enter a SO tag as character string, and number of tags are returned
tok<-gsub("(/| )","-",tok)
tok<-gsub("#","%23",tok,fixed=TRUE)
base.stack<-"http://stackoverflow.com/questions/tagged/"
stack.tree<-htmlTreeParse(paste(base.stack,tok,sep=""),useInternalNodes=TRUE)
tag.count<-getNodeSet(stack.tree,"//div[@class='module']/div[@class='summarycount al']")
tag.num<-suppressWarnings(as.numeric(gsub(",","",xmlValue(tag.count[[1]]),fixed=TRUE)))
if(is.na(tag.num)) {
warning(paste("Something went wrong trying to parse '",tok,"'.\nNA returned",sep=""))
}
return(tag.num)
}
#call it like this
get.stack("python")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment