parse xml document (on url) in python
Question: I am trying to parse xml document (URL) using requests,

facing following error:

    
    
    ValueError: Unicode strings with encoding declaration are not supported
    

here is my code:

    
    
    import requests
    from lxml import etree
    from lxml.etree import fromstring
    
    req = requests.request('GET', "http://www.nbp.pl/kursy/xml/LastC.xml")
    
    a = req.text
    b = etree.fromstring(a)
    

how can i get this xml parsed. Thanks in advance for help


Answer: You are passing in the _Unicode decoded version_. Don't do that, XML parsers
require that you pass in the raw bytes instead.

Instead of `req.text`, use `req.content` here:

    
    
    a = req.content
    b = etree.fromstring(a)
    

You can also _stream_ the XML document to the parser:

    
    
    req = requests.get("http://www.nbp.pl/kursy/xml/LastC.xml", stream=True)
    req.raw.decode_content = True  # ensure transfer encoding is honoured
    b = etree.parse(req.raw)
    

