使用 xml.etree.ElemenTree 和请求时出现 Python TypeError [英] Python TypeError while using xml.etree.ElemenTree and requests

查看:21
本文介绍了使用 xml.etree.ElemenTree 和请求时出现 Python TypeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这对我有用:

<前><代码>导入 xml.etree.ElementTree 作为 ET从 urllib2 导入 urlopenurl = 'http://example.com'# 这个 url 指向一个 `xml` 页面树 = ET.parse(urlopen(url))

然而,当我切换到 requests 时,出了点问题:

<前><代码>进口请求导入 xml.etree.ElementTree 作为 ETurl = 'http://example.com'# 这个 url 指向一个 `xml` 页面树 = ET.parse(requests.get(url))

引用错误如下所示:

<前><代码>---------------------------------------------------------------------------TypeError Traceback(最近一次调用最后一次)在 ()----> 1 棵树 = 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解析(来源,解析器=无):第 1181 章-> 1182 tree.parse(源代码,解析器)1183回树1184/usr/lib/python2.7/xml/etree/ElementTree.py 在 parse(self, source, parser)第645话第646话--> 647 source = open(source, "rb")第648话649尝试:类型错误:强制转换为 Unicode:需要字符串或缓冲区,找到响应

所以,我的问题是:在我的情况下,requests 有什么问题,我怎样才能让它在 ETrequests 中工作?

解决方案

您正在将 requests respones object 传递给 ElementTree;您想传入 原始文件对象相反:

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)>>>树 = ET.parse(r.raw)>>>树<xml.etree.ElementTree.ElementTree 对象在 0x109dadc50>>>>tree.getroot().tag'spocosy'

如果你有一个压缩的 URL,原始套接字(如 urllib2)返回未解码的压缩数据;在这种情况下,您可以在 二进制响应内容:

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

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?

解决方案

You are passing the requests respones object to ElementTree; you want to pass in the raw file object instead:

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

.raw returns the 'file-like' socket object, from which ElementTree.parse() will read, just like it'll read from the urllib2 response (which is itself a file-like object).

Concrete example:

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

If you have a compressed URL, the raw socket (like urllib2) returns the compressed data undecoded; in that case you can use the ET.fromstring() method on the binary response content:

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

这篇关于使用 xml.etree.ElemenTree 和请求时出现 Python TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆