Last active
August 29, 2015 14:22
-
-
Save mamonu/01eb2c902ac236234c2a to your computer and use it in GitHub Desktop.
get Stack Overflow programming language use data
This file contains hidden or 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
| # 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