Instantly share code, notes, and snippets.

Embed
What would you like to do?
Extracting Google Suggest Keyword
#autoinstall packages
packages <- c("XML", "httr")
if (length(setdiff(packages, rownames(installed.packages()))) > 0) {
install.packages(setdiff(packages, rownames(installed.packages())))
}
# Enjoy learning ? https://dataseolabs.com
library(httr)
library(XML)
# put your query
query <- URLencode("comment devenir")
url <- paste0("http://suggestqueries.google.com/complete/search?output=toolbar&hl=en&q=",query)
# use GET method
req <- GET(url)
# extract xml
xml <- content(req)
# parse xml
doc <- xmlParse(xml)
# extract attributes from
# <CompleteSuggestion><suggestion data="XXXXXX"/></CompleteSuggestion>
list <- xpathSApply(doc, "//CompleteSuggestion/suggestion", xmlGetAttr, 'data')
# print results
print(list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment