D
D
Dmitry Sopot2017-08-30 13:01:21
Django
Dmitry Sopot, 2017-08-30 13:01:21

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')
'

below is my code:
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)

I steamed the page, and I get all the currencies and rates for them
, how to implement them so that they come out in pairs, CURRENCY - RATE

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Dugin, 2017-10-06
@0dminik

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 question

Ask a Question

731 491 924 answers to any question