使用 xml.etree.ElemenTtree 和请求时出现 Python TypeError

标签 python xml urllib2 python-requests

这对我有用:


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

但是,当我切换到请求时,出现了问题:


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 出了什么问题,如何才能使其与 requests 一起工作 ET

最佳答案

您正在将请求响应对象传递给ElementTree;你想传递raw file object相反:

r = requests.get(url, stream=True)
ET.parse(r.raw)

.raw 返回“类似文件”的套接字对象,ElementTree.parse() 将从中读取,就像从 中读取一样urllib2 响应(它本身就是一个类似文件的对象)。

具体例子:

>>> r = requests.get('http://www.enetpulse.com/wp-content/uploads/sample_xml_feed_enetpulse_soccer.xml', stream=True)
>>> tree = ET.parse(r.raw)
>>> tree
<xml.etree.ElementTree.ElementTree object at 0x109dadc50>
>>> tree.getroot().tag
'spocosy'

如果您有压缩的 URL,原始套接字(如 urllib2)会返回未解码的压缩数据;在这种情况下,您可以在 binary response content 上使用 ET.fromstring() 方法:

r = requests.get(url)
ET.fromstring(r.content)

关于使用 xml.etree.ElemenTtree 和请求时出现 Python TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16933637/

相关文章:

python - 在 Python Flask 中将 Pandas 数据框作为 JSONP 响应返回

python - 将文本数据库分成 N 个相等的 block 并保留标题

python - 如何在 python 中删除字符串的第一部分

xml - Flex 和嵌入式 XML

c# - 从流中读取 XML

python - 在包含特殊重音字符的字符串的字符之间添加空格

xml - 为什么将对象列表导出到 CSV 会返回 System.Object[]?

python - Feedparser(和 urllib2)问题 : Connection timed out

python - 通过urllib和python下载图片

python - 用 Python 抓取城市词典