Skip to content

Instantly share code, notes, and snippets.

@cuducos
Last active January 3, 2020 21:11
Show Gist options
  • Save cuducos/1ab59c9080f93f9aa30ec38b732cd3c0 to your computer and use it in GitHub Desktop.
Save cuducos/1ab59c9080f93f9aa30ec38b732cd3c0 to your computer and use it in GitHub Desktop.
Cartões Corporativos
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@diraol
Copy link

diraol commented Dec 30, 2019

Não consegui terminar ainda, mas tá aqui uma função que pega o valor corrigido via IPCA (mensal):

import re

import requests
from bs4 import BeautifulSoup

def corrige_valor(valor_original, mes_origem, ano_origem, mes_final=11, ano_final=2019):

    """
    Função que corrige, de acordo com o IPCA, um valor monetário (float).

    Args:
        - valor_original (float): valor a ser corrigido que está na data de origem.
        - mes_origem (int): Número do mês de origem (1 a 12)
        - ano_origem (int): Ano de origem
        - mes_final (int): Número do mês para o qual o valor deverá ser corrigido (1 a 12): default: 11
        - ano_final (int): Ano para o qual o valor deverá ser corrigido - default: 2019
    Return:
        Valor corrigido
    """

    resp = requests.post('https://www3.bcb.gov.br/CALCIDADAO/publico/corrigirPorIndice.do?method=corrigirPorIndice',
                         data={'aba':1,
                               'selIndice': '00433IPCA',
                               'dataInicial': f'{mes_origem:0>2d}/{ano_origem}',
                               'dataFinal': f'{mes_final:0>2d}/{ano_final}',
                               'valorCorrecao': valor_original,
                               'idIndice': None,
                               'nomeIndicePeriodo': None
                              })
    valor = BeautifulSoup(resp.content)\
        .select('table[title^="Correção de valores"]', class_="tabela")[0]\
        .select('tbody')[0]\
        .select('td')[-1]\
        .text
    valor = float(re.findall(r"\d+,\d\d", valor.replace(".", ""))[0].replace(",", "."))
    return valor

Agora precisaria, das duas uma:

1 - Fazer a sumarização por mês/ano, fazer a correção e depois "juntar tudo"
2 - Fazer a correção para cada dado e depois "juntar tudo".

Como a função faz chamadas (http post) para cada vez que é chamada, o melhor é "agregar por mês", corrigir o valor e depois finalizar a sumarização.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment