I am scraping the following website: https://www.banorte.com/wps/portal/ixe/Home/indicadores/tipo-de-cambio
I am trying to get the table of currency exchange rates into an R data frame via the rvest package, but the table itself is configured in a JavaScript variable within the HTML code.
I located the relevant css selector and now I have this:
library(rvest)
banorte <- "https://www.banorte.com/wps/portal/ixe/Home/indicadores/tipo-de-cambio/" %>%
read_html() %>%
html_nodes('#indicadores_financieros_wrapper > script:nth-child(2)')
my output is now the following JavaScript script, as an XML nodeset:
<script>
$(document).ready(function(){
var valor = '{"tablaDivisas":[{"nombreDivisas":"FRANCO SUIZO","compra":"18.60","venta":"19.45"}, {"nombreDivisas":"LIBRA ESTERLINA","compra":"24.20","venta":"25.15"}, {"nombreDivisas":"YEN JAPONES","compra":"0.1635","venta":"0.171"}, {"nombreDivisas":"CORONA SUECA","compra":"2.15","venta":"2.45"}, {"nombreDivisas":"DOLAR CANADA","compra":"14.50","venta":"15.35"}, {"nombreDivisas":"EURO","compra":"21.75","venta":"22.60"}], "tablaDolar":[{"nombreDolar":"VENTANILLA","compra":"17.73","venta":"19.15"}]}';
if(valor != '{}'){
var objJSON = eval("(" + valor + ")");
var tabla="<tbody>";
for ( var i = 0; i < objJSON["tablaDolar"].length; i++) {
tabla+= "<tr>";
tabla+= "<td>" + objJSON["tablaDolar"][i].nombreDolar + "</td>";
tabla+= "<td>$" + objJSON["tablaDolar"][i].compra + "</td>";
tabla+= "<td>$" + objJSON["tablaDolar"][i].venta + "</td>";
tabla+= "</tr>";
}
tabla+= "</tbody>";
$("#tablaDolar").append(tabla);
var tabla2="";
for ( var i = 0; i < objJSON["tablaDivisas"].length; i++) {
tabla2+= "<tr>";
tabla2+= "<td>" + objJSON["tablaDivisas"][i].nombreDivisas + "</td>";
tabla2+= "<td>$" + objJSON["tablaDivisas"][i].compra + "</td>";
tabla2+= "<td>$" + objJSON["tablaDivisas"][i].venta + "</td>";
tabla2+= "</tr>";
}
tabla2+= "</tbody>";
$("#tablaDivisas").append(tabla2);
}
bmnIndicadoresResponsivoInstance.cloneResponsive(0);
});
</script>
My question is, how do I remove almost everything (all the JavaScript functions/operators) to get only this data with the intention of converting it eventually to a JSON table like this:
{"tablaDivisas":[{"nombreDivisas":"FRANCO SUIZO","compra":"18.60","venta":"19.45"},
{"nombreDivisas":"LIBRA ESTERLINA","compra":"24.20","venta":"25.15"},
{"nombreDivisas":"YEN JAPONES","compra":"0.1635","venta":"0.171"},
{"nombreDivisas":"CORONA SUECA","compra":"2.15","venta":"2.45"},
{"nombreDivisas":"DOLAR CANADA","compra":"14.50","venta":"15.35"},
{"nombreDivisas":"EURO","compra":"21.75","venta":"22.60"}],
"tablaDolar":[{"nombreDolar":"VENTANILLA","compra":"17.73","venta":"19.15"}]}
In other words, I need to extract the "valor" variable from the JS script using R.
For some reason I've had trouble getting this done all within R (without having to export the variable as an external .txt file and then using a substring)