This works for me:
import xml.etree.ElementTree as ET
from urllib2 import urlopen
url = 'http://example.com'
# this url points to a `xml` page
tree = ET.parse(urlopen(url))
However, when I switch to requests
, something was wrong:
import requests
import xml.etree.ElementTree as ET
url = 'http://example.com'
# this url points to a `xml` page
tree = ET.parse(requests.get(url))
The trackback error is showed below:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in ()
----> 1 tree = ET.parse(requests.get(url, proxies={'http': '192.168.235.36:7788'}))
/usr/lib/python2.7/xml/etree/ElementTree.py in parse(source, parser)
1180 def parse(source, parser=None):
1181 tree = ElementTree()
-> 1182 tree.parse(source, parser)
1183 return tree
1184
/usr/lib/python2.7/xml/etree/ElementTree.py in parse(self, source, parser)
645 close_source = False
646 if not hasattr(source, "read"):
--> 647 source = open(source, "rb")
648 close_source = True
649 try:
TypeError: coercing to Unicode: need string or buffer, Response found
So, my question is: wha is wrong with requests
in my situation and how can I make it work ET
with requests
?