Reputation: 82755
Hi i am able to parse a normal xml like xml = lxml.etree.parse(''http://abc.com/A.xml') but now i have this path authenticated with a user name and password is it possible to input the username and password and parse the url, like in connecting a database where you can give the user name password in the connection string
Upvotes: 1
Views: 2431
Reputation: 82755
Guys i found a way to parse password protected XML this is what i did.
import urllib2
import base64
theurl = 'http://abc.com/A.xml'
username='AAA'
password='BBB'
req = urllib2.Request(theurl)
base64string = base64.encodestring(
'%s:%s' % (username, password))[:-1]
authheader = "Basic %s" % base64string
req.add_header("Authorization", authheader)
try:
handle = urllib2.urlopen(req)
except IOError, e:
print "It looks like the username or password is wrong."
xml = handle.read()
inputXml = etree.fromstring(xml)
Upvotes: 3
Reputation: 4688
Yes, it's possible. Before parsing the XML document with lxml
, you need to get it by making an HTTP request that handles the HTTP Basic/Digest Authentication properly. For example, with urllib2.HTTPBasicAuthHandler
like in this solution: Python urllib2 HTTPBasicAuthHandler
Upvotes: 3