# Grab Google Suggest Search Queries using R'

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

* &#x20;`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

```r
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)
}
​
```

```r
suggestGSQueries <- function (search_query, code_lang, level) {
  if(length(search_query) == 1){
  all_suggestion <- getGSQueries(search_query, code_lang)
  message("level 1")
  
  if (level > 1) {
    for (l in letters) {
      message("level 2 ", l)
      Sys.sleep(runif(1, 0, 2))
      local_suggestion <-
        getGSQueries(paste0(search_query," ", l), code_lang)
      all_suggestion <- c(all_suggestion, local_suggestion)
      
    }
    
    if (level > 2) {
      for (l1 in letters) {
        for (l2 in letters) {
          Sys.sleep(1+runif(1, 0, 9))
          message("level 3 ", l1, l2)
          local_suggestion <-
            getGSQueries(paste0(search_query," ", l1, l2), code_lang)
          all_suggestion <- c(all_suggestion, local_suggestion)
          
        }
        
      }
    }
  }
  
  all_suggestion <- unique(all_suggestion)
  } else {
    message(1," ",search_query[1])
    all_suggestion <- getGSQueries(as.character(search_query[1]), code_lang)
    for (word in 2:length(search_query)){
      Sys.sleep(1+runif(1, 0, 9))
      message(word," ",search_query[word])
      all_suggestion <- c(all_suggestion,getGSQueries(as.character(search_query[word]), code_lang))
    }

    all_suggestion
  }
}
```

This is how you can use it:

```r
kwd <- suggestGSQueries('covid', 'en', 2)
​
View(as.data.frame(unlist(kwd)))
```

The first parameter is the *seed* keyword, the second one is the language (or [host language](https://developers.google.com/custom-search/docs/xml_results?hl=en#WebSearch_Query_Parameter_Definitions:~:text=pizza%26gl%3Duk-,hl,-Description)), and the last one is the level of details (1,2 or 3).&#x20;

**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.&#x20;

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

```r
deeper_kwd <- suggestGSQueries(kwd, 'en', 1)

View(as.data.frame(unlist(deeper_kwd)))
```

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.rforseo.com/apis/grab-google-suggest-search-queries-using-r.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
