🤖
R for SEO
  • Using R for SEO, What to expect?
  • Getting started
  • What is R? What is SEO?
  • About this Book
  • Crawl and extract data
    • What's crawling and why is it useful?
    • Download and check XML sitemaps using R'
    • Crawling with rvest
    • Website Crawling and SEO extraction with Rcrawler
    • Perform automatic browser tests with RSelenium
  • Grabbing data from APIs
    • Grab Google Suggest Search Queries using R'
    • Grab Google Analytics Data x
    • Grab keywords search volume from DataForSeo API using R'
    • Grab Google Rankings from VALUE SERP API using R'
    • Classify SEO Keywords using GPT-3 & R'
    • Grab Google Search Console Data x
    • Grab 'ahrefs' API data x
    • Grab Google Custom search API Data x
    • Send requests to the Google Indexing API using googleAuthR
    • other APIs x
  • Export and read Data
    • Send and read SEO data to Excel/CSV
    • Send your data by email using gmail API
    • Send and read SEO data to Google Sheet x
  • data wrangling & analysis
    • Join Crawl data with Google Analytics Data
    • Count words, n-grams, shingles x
    • Hunt down keyword cannibalization
    • Duplicate content analysis x
    • Compute ‘Internal Page Rank’
    • SEO traffic Forecast x
    • URLs categorization
    • Track SEO active pages percentage over time x
  • Data Viz
    • Why Data visualisation is important? x
    • Use Esquisse to create plots quickly
  • Explore data with rPivotTable
  • Resources
    • Launch an R script using github actions
    • Types / Class & packages x
    • SEO & R People x
    • Execute R code online
    • useful SEO XPath's & CSS selectors X
Powered by GitBook
On this page

Was this helpful?

  1. Grabbing data from APIs

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)
}
​
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:

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

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 :)

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.

PreviousPerform automatic browser tests with RSeleniumNextGrab Google Analytics Data x

Last updated 2 years ago

Was this helpful?

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

host language