How to extract all "query" data (keywords of coffee) from this link: https://serpapi.com/search.html?engine=google_trends&q=coffee&data_type=RELATED_QUERIES&cat=0&date=now+7-d&api_key=317da75462cab4790705a5cf8b6a9c74c9ba9f279150afb87d4b191f95d8d5de
to be one column data frame in R. I didn’t get the result with rvest.
Regards
library(rvest)
allcom <- read_html("https://serpapi.com/search.html?engine=google_trends&q=coffee&data_type=RELATED_QUERIES&cat=0&date=now+7-d&api_key=317da75462cab4790705a5cf8b6a9c74c9ba9f279150afb87d4b191f95d8d5de")
allcom %>% html_attr("query")
[1] NA
2
Answers
You should use a json request instead of html and use the
httr2
andjsonlite
packages to easily convert to a dataframe:replace your url by "https://serpapi.com/search.json?engine=google_trends&q=coffee&data_type=RELATED_QUERIES&cat=0&date=now+7-d&api_key=317da75462cab4790705a5cf8b6a9c74c9ba9f279150afb87d4b191f95d8d5de" to get a JSON response.
I used the rjsoncons CRAN package to retrieve and ‘pivot’ the ‘related_queries.top’ JMESPath to a tibble
Actually to develop this answer I first explored the JSON using the listviewer package