Skip to content

Instantly share code, notes, and snippets.

@KarimullinArthur
Created April 10, 2024 08:20
Show Gist options
  • Save KarimullinArthur/bf51db27eba388ebe107f32510bb5031 to your computer and use it in GitHub Desktop.
Save KarimullinArthur/bf51db27eba388ebe107f32510bb5031 to your computer and use it in GitHub Desktop.
Parser of rate from CBR
import bs4 as bs
import requests
URL = 'http://www.cbr.ru/scripts/XML_daily.asp?date_req='
def get_rate(code: str, date: str = '') -> float:
date = reversed(date.split('-'))
date = '/'.join(str(x) for x in date)
resp = requests.get(URL+date).text
soup = bs.BeautifulSoup(resp, "xml")
chars = soup.find_all('CharCode')
rate_values = soup.find_all('VunitRate')
chars = list(map(lambda x: x.text, chars))
rate_values = list(map(lambda x: float(x.text.replace(',', '.')),
rate_values))
for rate_currency in rate_values:
if chars[rate_values.index(rate_currency)] == code:
return rate_currency
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment