Web Scraping API USD a PEN - Bloomberg

scraping

Escrapeamos APIs de Bloomber para ver cambio de monedas de Dólares a Soles

Keith Caballero https://keithstatsblog.netlify.app/
2022-03-05

Scrapeo de APIs de Bloomberg

Importamos Librerias

Show code
pacman::p_load(
  rvest,httr,jsonlite,tidyverse,lubridate,plotly
)

Creamos función ya que read_json nos genero errores de lectura

Show code
read_url <- function(url){
  GET(url,
      config = add_headers(.headers = c(
        "User-Agent" = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"
      ))
  ) %>% content()
  
}

APIs scrapeados

Show code
api <- "https://www.bloomberg.com/markets2/api/history/USDPEN%3ACUR/PX_LAST?"
api_semanal <- paste0(api,"timeframe=5_YEAR&period=weekly&volumePeriod=weekly")
api_diario <- paste0(api,"timeframe=5_YEAR&period=daily&volumePeriod=daily")

Generamos los gráficos

USD A PEN Semanalmente

Show code
(read_url(api_semanal) %>% 
  map_df("price") %>% 
  transmute(`DOL a PEN`=value,Fechas = ymd(dateTime)) %>% 
  ggplot(aes(Fechas,`DOL a PEN`)) +
  geom_line(size=0.6,alpha=0.7))

USD A PEN Diario

Show code
(read_url(api_diario) %>% 
    map_df("price") %>% 
    transmute(`DOL a PEN`=value,Fechas = ymd(dateTime)) %>% 
    ggplot(aes(Fechas,`DOL a PEN`)) +
    geom_line(size=0.6,alpha=0.7)) 

Corrections

If you see mistakes or want to suggest changes, please create an issue on the source repository.

Citation

For attribution, please cite this work as

Caballero (2022, March 5). Keith Stats Blog: Web Scraping API USD a PEN - Bloomberg. Retrieved from https://keithstatsblog.netlify.app/posts/2022-03-05-web-scraping-pen-bloomberg/

BibTeX citation

@misc{caballero2022web,
  author = {Caballero, Keith},
  title = {Keith Stats Blog: Web Scraping API USD a PEN - Bloomberg},
  url = {https://keithstatsblog.netlify.app/posts/2022-03-05-web-scraping-pen-bloomberg/},
  year = {2022}
}