Last active
August 29, 2015 14:02
-
-
Save datagistips/0516ab5410174e15e79f to your computer and use it in GitHub Desktop.
This file contains 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
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