Reputation: 7127
I have some URLs such as:
https://www.ine.es/jaxiT3/Tabla.htm?t=30656&L=0
https://www.ine.es/jaxiT3/Tabla.htm?t=30813&L=0
etc.
In the top right-hand corner of each of the links there is a download icon. After clicking it, it gives the option to download in a JSON format.
The JSON link looks like:
https://servicios.ine.es/wstempus/js/es/DATOS_TABLA/30656?tip=AM&
I can read one of the JSON URLs using:
library(jsonlite)
out <- fromJSON("https://servicios.ine.es/wstempus/js/es/DATOS_TABLA/30656?tip=AM&")
My question is, how can I extract all of the JSON URLs from each of the download links?
The button element is found using:
url <- "https://www.ine.es/jaxiT3/Tabla.htm?t=30656&L=0"
read_html(url) %>%
html_nodes("a") %>%
.[15]
However, I am not sure this will work across all URLs. Data:
Data <- structure(list(index = c("2.1.1", "2.1.2", "2.1.3", "2.1.4",
"2.1.5", "2.1.6"), title = c("Indicadores de renta media y mediana",
"Distribución por fuente de ingresos", "Porcentaje de población con ingresos por unidad de consumo por debajo de determinados umbrales fijos por sexo",
"Porcentaje de población con ingresos por unidad de consumo por debajo de determinados umbrales fijos por sexo y tramos de edad",
"Porcentaje de población con ingresos por unidad de consumo por debajo de determinados umbrales fijos por sexo y nacionalidad",
"Porcentaje de población con ingresos por unidad de consumo por debajo/encima de determinados umbrales relativos por sexo"
), link = c("https://www.ine.es/jaxiT3/Tabla.htm?t=30656&L=0",
"https://www.ine.es/jaxiT3/Tabla.htm?t=30813&L=0", "https://www.ine.es/jaxiT3/Tabla.htm?t=30657&L=0",
"https://www.ine.es/jaxiT3/Tabla.htm?t=30659&L=0", "https://www.ine.es/jaxiT3/Tabla.htm?t=30660&L=0",
"https://www.ine.es/jaxiT3/Tabla.htm?t=30661&L=0"), provincia = c("Albacete",
"Albacete", "Albacete", "Albacete", "Albacete", "Albacete")), row.names = c(NA,
6L), class = "data.frame")
Upvotes: 2
Views: 191
Reputation: 84465
They have a common structure by the looks of it. Just extract the t
param ,for each link in the link
column, and put that into a json url template string:
library(stringr)
Data$`json_link` <- lapply(Data$link, function(x) {sprintf('https://servicios.ine.es/wstempus/js/es/DATOS_TABLA/%s?tip=AM&', stringr::str_match(x, 't=(\\d+)')[,2])} )
Upvotes: 1