Skip to content

Instantly share code, notes, and snippets.

@thiagomarzagao
Last active August 13, 2020 11:55
Show Gist options
  • Save thiagomarzagao/490680c2673f2c893fc24ae5a6a3d7ff to your computer and use it in GitHub Desktop.
Save thiagomarzagao/490680c2673f2c893fc24ae5a6a3d7ff to your computer and use it in GitHub Desktop.
script que usa a Calculadora do Cidadão do BACEN p/ corrigir valores pela inflação
import re
import time
import pandas as pd
from random import random
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# make waits random to make our bot a bit more human-like
def wait(constant = 0.1):
time.sleep(constant + random())
# make Chrome run in the background
chrome_options = Options()
chrome_options.add_argument("--headless")
# launch Chrome
path_to_chromedriver = '/path/to/chromedriver'
browser = webdriver.Chrome(
executable_path = path_to_chromedriver,
options = chrome_options
)
# dates and values you want to adjust for inflation
# (more likely you'd get this from some CSV file)
# notice formats:
# monthyear, with two-digit months
# decimal points included
data = [
('011968', 123456), # jan/1968, NCr$ 1234.56
('021980', 654321), # feb/1980, Cr$ 6543.21
('041989', 121212), # apr/1984, NCz$ 1212.12
# ...
]
# final date
t1 = '062020' # i.e., adjust for inflation until jun/2020
# Caculadora do Cidadao
url = 'https://www3.bcb.gov.br/CALCIDADAO/publico/exibirFormCorrecaoValores.do?method=exibirFormCorrecaoValores'
updated_array = []
for tup in data:
t0, amount = tup
# go to Calculadora do Cidadao
browser.get(url)
# pick an inflation index (here I'm choosing the oldest one)
dropdown = browser.find_element_by_id('selIndice')
for option in dropdown.find_elements_by_tag_name('option'):
if option.text == 'IGP-DI (FGV) - a partir de 02/1944': # change as needed
option.click()
break
wait()
# enter initial month/year
browser.find_element_by_name('dataInicial').send_keys(t0)
wait()
# enter final month/year
browser.find_element_by_name('dataFinal').send_keys(t1)
wait()
# enter amount to be updated, including two decimal points
browser.find_element_by_name('valorCorrecao').send_keys(amount)
wait()
# run it!
browser.find_element_by_css_selector("[title^='Corrigir valor']").click()
wait(2)
# get inflation-adjusted ibovespa
updated = browser.find_elements_by_xpath("//*[contains(text(), 'Valor corrigido')]/following-sibling::td")[0].text
updated = int(re.sub('[^0-9]', '', updated))/100
updated_array.append((t0, amount/100, updated))
print(t0, updated)
df = pd.DataFrame(updated_array)
df.columns = ['date', 'amount_t0', 'amount_t1']
df.to_csv('output.csv', index = False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment