Skip to content

Instantly share code, notes, and snippets.

@drewconway
Created January 22, 2011 22:26
Show Gist options
  • Save drewconway/791559 to your computer and use it in GitHub Desktop.
Save drewconway/791559 to your computer and use it in GitHub Desktop.
Function takes a string as parameter and returns the approximate number of Google search results containing that string
require(RCurl)
require(XML)
google.counts<-function(s){
search.url<-paste("http://www.google.com/search?q=",gsub(" ","+",s),sep="")
search.html<-getURL(search.url)
parse.search<-htmlTreeParse(search.html,useInternalNodes = TRUE)
search.nodes<-getNodeSet(parse.search,"//div[@id='resultStats']")
search.value<-strsplit(xmlValue(search.nodes[[1]])," ",fixed=TRUE)[[1]][2]
return(as.numeric(gsub(",","",search.value,fixed=TRUE)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment