S
S
Sergey Frolov2020-04-27 11:12:13
Python
Sergey Frolov, 2020-04-27 11:12:13

Unicode strings with encoding declaration are not supported - how can I fix it?

Tell a newbie about the code:

try:
    url = 'http://autoconfig.' + domain + '/mail/config-v1.1.xml'
    res = requests.get(url)
    doc = etree.fromstring(res.text)
    strpath = '//outgoingServer[@type="{}"]'.format('smtp')
    a = doc.xpath(strpath)
    elem = a[0].getchildren()
    serv = elem[0].text
    port = elem[1].text
    return serv + ':' + port
  except Exception as e:
    print e
    True


Print error - Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration.

I seem to understand that I need to do encoding='utf-8' ..But how to make it work ..?
Tell.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Grebennikov, 2020-04-27
@SergeyGrebennikov

Try

doc = etree.fromstring(bytes(res.text, encoding='utf-8'))

Also, do you write code compatible with Python version 2? print eIf not , then correctprint(e)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question