D
D
Daulet2015-01-04 20:08:26
Python
Daulet, 2015-01-04 20:08:26

Python: how to parse this with lxml?

How to parse this piece using the lxml (python)
code library

<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
<gesmes:subject>Reference rates</gesmes:subject>
<gesmes:Sender>
<gesmes:name>European Central Bank</gesmes:name>
</gesmes:Sender>
<Cube>
<Cube time="2015-01-02">
<Cube currency="USD" rate="1.2043"/>
<Cube currency="JPY" rate="145.21"/>
<Cube currency="BGN" rate="1.9558"/>
<Cube currency="CZK" rate="27.693"/>
<Cube

when I write tree.xpath('/gesmes:Envelope/Cube/Cube')
error: lxml.etree.XPathEvalError: Undefined namespace prefix

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
alternativshik, 2015-01-05
@Danil38

Namespaces in lxml lxml.de/tutorial.html#namespaces

Y
Yuri Shikanov, 2015-01-04
@dizballanze

You can try lxml.etree.iterparse

D
Daulet, 2015-01-04
@Danil38

still returns empty sheet...
so i get tree

# читаем страницу с сети
        xml_obj = urllib2.urlopen(self.source_url)
        xml_str = xml_obj.read()

        # import io
        # xml_str = io.BytesIO(xml_str)
        # строим дерево
        tree = etree.XML(xml_str)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question