Sravani Chinta
Sravani Chinta

Reputation: 71

Read attribute data from an xml in python

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

Upvotes: 0

Views: 80

Answers (2)

Bill F
Bill F

Reputation: 731

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")

Upvotes: 0

Nishant Nawarkhede
Nishant Nawarkhede

Reputation: 8400

et.parse requires file path (not contents).

You need to encode response to utf-8

xml_data = et.fromstring(response.text.encode('utf-8'))

Upvotes: 1

Related Questions