Skip to content

Instantly share code, notes, and snippets.

@michelts
Created December 1, 2021 22:25
Show Gist options
  • Save michelts/022fb01e5b9830442fd224f1b05dfde9 to your computer and use it in GitHub Desktop.
Save michelts/022fb01e5b9830442fd224f1b05dfde9 to your computer and use it in GitHub Desktop.
import lxml
import requests
url = "https://www3.bcb.gov.br/sgspub/consultarvalores/consultarValoresSeries.do?method=consultarValores"
payload = {
"optSelecionaSerie": "7460",
"dataInicio": "31/01/1994",
"dataFim": "01/12/2021",
"selTipoArqDownload": "1",
"chkPaginar": "on",
"hdOidSeriesSelecionadas": "7460",
"hdPaginar": "true",
"bilServico": "[SGSFW2301]",
}
args = {"desired": "2021/12"}
def main():
response = get_response_from_last_page()
for row in get_valid_rows(response):
month, value = parse_row(row)
print(month, value)
def get_response_from_last_page():
for i in range(1000):
response = requests.post(url, payload)
if search_desired_month(response):
return response
else:
raise ValueError('Page not found for month "desired"')
def get_valid_rows(response):
root = parse_html(response.content)
table = root.find_by_css("#valoresSeries")
for row in table.find_by_css("tbody tr"):
if row.has_class("fundoPadraoAClaro3") or row.has_class("fundoPadraoAClaro2"):
yield row
def parse_row(row):
month, value = row.find_by_css("td")
return parse_month(month), convert_to_float(value)
def search_desired_month(response):
latest_month = get_latest_stored_month()
return latest_month in response.content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment