verginio

bcb_estabelecimentos_financeiros

Feb 16th, 2021 (edited)
553
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #install.packages("tidyverse")
  2. #install.packages("rvest")
  3. #install.packages("xml2")
  4.  
  5. library(tidyverse)
  6. #library(rvest)
  7. #library(xml2)
  8.  
  9. set_path <- "C:/dadosR/BCB/Estabelecimentos_Financeiros"
  10.  
  11.  
  12. # files
  13.  
  14. link_bcb <- "https://www4.bcb.gov.br"
  15. link_estban <- paste0(link_bcb, "/fis/cosif/estban.asp?frame=1")
  16.  
  17. pg <- xml2::read_html(link_estban)
  18.  
  19. df_files <- pg %>%
  20.   rvest::html_nodes("option") %>%
  21.   rvest::html_attr("value") %>%
  22.   enframe(value = "link_zip") %>%
  23.   mutate_at("link_zip", ~ paste0(link_bcb, .x)) %>%
  24.   mutate(file_name_zip = basename(link_zip)) %>%
  25.   mutate(file_name_csv = str_replace(file_name_zip, ".ZIP", ".CSV")) %>%
  26.   mutate(ano_mes = str_remove(file_name_zip, "_.*")) %>%
  27.   mutate(ano = str_sub(ano_mes, 1, 4)) %>%
  28.   mutate(mes = str_sub(ano_mes, 5, 6)) %>%
  29.   mutate(tipo = case_when(
  30.     str_detect(link_zip, "municipio") ~ "municipio",
  31.     str_detect(link_zip, "agencia") ~ "agencia",
  32.     T ~ "outros"
  33.   ))
  34.  
  35. df_files
  36.  
  37.  
  38. # select files
  39. df_files_select <- df_files %>%
  40.   filter(tipo == "municipio") %>%
  41.   filter(ano == 2020)
  42.  
  43. df_files_select
  44.  
  45.  
  46.  
  47.  
  48. # download and extract
  49. setwd(set_path)
  50.  
  51.  
  52. fun_download <- function(.i, .df) {
  53.   print(.i)
  54.  
  55.   link_zip <- .df$link_zip[.i]
  56.   file_zip <- .df$file_name_zip[.i]
  57.   file_csv <- .df$file_name_csv[.i]
  58.  
  59.   if (!file.exists(file_zip)) {
  60.     download.file(
  61.       url = link_zip,
  62.       destfile = file_zip,
  63.       mode = "wb"
  64.     )
  65.    
  66.     Sys.sleep(2)
  67.   }
  68.  
  69.   if (!file.exists(file_csv)) unzip(file_zip)
  70.  
  71.  
  72.   print("ok")
  73. }
  74.  
  75.  
  76. result <- map(1:nrow(df_files_select),  ~ fun_download(.x, df_files_select))
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×