这对我有用:
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))
但是,当我切换到 时requests
,出现了问题:
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))
引用错误如下所示:
---------------------------------------------------------------------------
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
所以,我的问题是:我的情况有什么问题requests
,我怎样才能让它工作?ET
requests