A
A
Alexander2015-01-10 00:26:36
Python
Alexander, 2015-01-10 00:26:36

How to parse xml page?

Hello, I recently started learning python, I set myself the task of writing a currency parser, I managed to parse html from one site, but this is not at all, I need official data, but I can’t parse from here www.cbr.ru/scripts/XML_daily.asp ?date_req=02/03/2002 data. If it's not difficult to show a small piece of code how to parse? At least how to read the full page, otherwise errors constantly appear)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Druzhaev, 2015-01-10
@borodaturan

As I understand it, you need to get the exchange rate of the Belarusian ruble. This is easy to do using xpath using the lxml library.

import urllib

import lxml.etree as etree


def get_xml(url):
    response = urllib.urlopen(url)
    return response.read()

if __name__ == '__main__':
    xml = get_xml('http://www.cbr.ru/scripts/XML_daily.asp?date_req=02/03/2002')

    xml_data = etree.fromstring(xml)
    bel_rub = xml_data.xpath("/ValCurs/Valute[@ID='R01090']/Value")[0].text

    print(bel_rub)

N
NeX, 2015-01-10
@NeX

e.g. www.crummy.com/software/BeautifulSoup/bs4/doc

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question