Skip to content

Instantly share code, notes, and snippets.

@datagistips
Last active August 29, 2015 14:02
Show Gist options
  • Save datagistips/0516ab5410174e15e79f to your computer and use it in GitHub Desktop.
Save datagistips/0516ab5410174e15e79f to your computer and use it in GitHub Desktop.
library(RCurl)
library(rjson)
whatGoogleSuggestsFor <- function(question, domain="fr") {
# Domain : here, just France or USA
# You could add URLs for some other countries
if (domain == "fr") {
baseU <- "https://www.google.fr/s?gs_rn=45&sclient=psy-ab"
} else {
baseU <- "https://www.google.com/s?hl=en&gs_ri=psy-ab"
}
# create the URL based on the question
u <- paste(baseU, "&q=", URLencode(tolower(question)), sep="")
# get the Data
j <- fromJSON(getURL(u))
# structure it
unL <- unlist(j)
res <- sub("^.*<b>(.*)</b>$", "\\1", unL[setdiff(grep(question, unL), which(unL==question))])
print(res)
return(res)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment