Answer the question
In order to leave comments, you need to log in
How to combine the received data into a single one?
Write a simple CentralBankCurrency class that, after initializing the object, allows you to get its current rate by the attribute corresponding to the currency code in lower case. The data is taken from the official site via XML.
www.cbr.ru/scripts/XML_daily.asp
For implementation, we recommend using the standard xml module and the requests library.
Example:
'
>>> currency = CentralBankCurrency()
>>> currency.usd
Decimal('59.5415')
>>> currency.eur
Decimal('67,6868')
'
import urllib.request, urllib.parse, urllib.error
import xml.etree.ElementTree as ET
url = 'https://www.cbr.ru/scripts/XML_daily.asp'
uh = urllib.request.urlopen(url)
data = uh.read()
#print(data)
tree = ET.fromstring(data)
name = tree.findall('.//CharCode')
for naz in name:
print(naz.text)
summa = tree.findall('.//Value')
for stoim in summa:
print(stoim.text)
Answer the question
In order to leave comments, you need to log in
for symbol, rate in zip(tree.findall('.//CharCode'), tree.findall('.//Value')):
print(symbol.text, rate.text)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question