Grab Google Suggest Search Queries using R'

To make things easier, I've created two dedicated functions:

  • getGSQueries this one grabs the queries

  • suggestGSQueries this one merges each request's results

Just copy and paste those 2 functions inside your RStudio Console

getGSQueries <- function (search_query, code_lang) {
  packages <- c("XML", "httr")
  if (length(setdiff(packages, rownames(installed.packages()))) > 0) {
    install.packages(setdiff(packages, rownames(installed.packages())))
  }
  library(httr)
  library(XML)
  
  
  query <- URLencode(search_query)
  url <-
    paste0(
      "http://suggestqueries.google.com/complete/search?output=toolbar&hl=",
      code_lang,
      "&q=",
      query
    )
  
  
  # message(url)
  # use GET method
  req <- GET(url)
  # extract xml
  
  # message(req$status_code)
  
  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)
  return(list)
}
​

This is how you can use it:

The first parameter is the seed keyword, the second one is the language (or host language), and the last one is the level of details (1,2 or 3).

1 will just grab the first suggestion list, 2 will grab suggestions if you add another letter ('covid a', 'covid b', 'covid c', ...), 3, which I don't recommend, will add two letters (covid aa, covid ab, ..) it's also possible to pass a vector instead of a string. In this example, we ask for Google suggestions for each of the results in the previous step.

it will drastically increase the keyword list and... it might a little bit of time too :)

Use these functions with caution because they can send a lot of queries to Google and you might get your IP banned.

Last updated

Was this helpful?