0

I am trying to read data from an xml file from an url using request module in python

import requests
from requests.auth import HTTPBasicAuth
import xml.etree.ElementTree as et
url ="https://sample.com/simple.xml"
response = requests.get(url,auth=HTTPBasicAuth(username,password))
xml_data = et.fromstring(response.text)

The error I am getting is:

Traceback (most recent call last):
  File "C:\Python27\myfolder\Artifactory.py", line 156, in <module>
    xml_data = et.fromstring(xml_response.text)
  File "C:\Python27\lib\xml\etree\ElementTree.py", line 1311, in XML
    parser.feed(text)
  File "C:\Python27\lib\xml\etree\ElementTree.py", line 1657, in feed
    self._parser.Parse(data, 0)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xae' in position 8419: ordinal not in range(128)

So i changed the code to xml_data = et.parse(response.text) then the error is :

Traceback (most recent call last):
  File "C:\Python27\myfolder\Artifactory.py", line 156, in <module>
    xml_data = et.parse(xml_response.text)
  File "C:\Python27\lib\xml\etree\ElementTree.py", line 1182, in parse
    tree.parse(source, parser)
  File "C:\Python27\lib\xml\etree\ElementTree.py", line 647, in parse
    source = open(source, "rb")
IOError: [Errno 2] No such file or directory: u'<?xml version="1.0" encoding="utf-8"?>

After this error the xml data is getting printed please help me in this issue

2 Answers 2

1

et.parse requires file path (not contents).

You need to encode response to utf-8

xml_data = et.fromstring(response.text.encode('utf-8'))
Sign up to request clarification or add additional context in comments.

2 Comments

when i added the above mentioned solution<Element 'manifest' at 0x376fab0>. Is there any difference between both.Both the solutions are working
the difference is this is set only for that encoding. the other one sets it for the entire script.
0

The first attempt seems like an encoding issue with python.

try adding this to your code between your last import and your url variable.

import sys
reload(sys)
sys.setdefaultencoding("utf8")

1 Comment

i added and when i printing the result <Element 'manifest' at 0x42bbab0>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.