Python ElementTree XML IOError:[Errno 22]无效模式('rb')或文件名 [英] Python ElementTree XML IOError: [Errno 22] invalid mode ('rb') or filename
问题描述
使用以下代码:
import xml.etree.cElementTree as ET
tree = ET.parse(r'https://apitest.batchbook.com/api/v1/people.xml?auth_token=GR5doLv88FrnLyLGIwok')
我收到错误消息:
IOError Traceback (most recent call last)
<ipython-input-10-d91d452da3e7> in <module>()
----> 1 tree = ET.parse(r'https://apitest.batchbook.com/api/v1/people.xml?auth_token=GR5doLv88FrnLyLGIwok')
<string> in parse(source, parser)
<string> in parse(self, source, parser)
IOError: [Errno 22] invalid mode ('rb') or filename: 'https://apitest.batchbook.com/api/v1/people.xml?auth_token=GR5doLv88FrnLyLGIwok'
但是,如果我在浏览器中打开上面的链接并保存了到XML文件(people.xml),然后执行以下操作:
However, if I open the link above in a browser, and save this to an XML-file (people.xml), and then do:
tree = ET.parse(r'C:\Users\Eric\Downloads\people.xml')
tree.getroot()
我得到结果:<元素'people'位于0x00000000086AA420>
I get the result: <Element 'people' at 0x00000000086AA420>
关于为什么使用链接无效的任何线索?谢谢:)
Any clue as to why using the link does not work? Thanks :)
推荐答案
在您的文件系统中任何地方都没有该名称的文件。 etree
不了解这确实是一个网址,即使使用它也无法执行任何操作。
There is no file of that name anywhere in your filesystem. etree
does not understand that this is really a web address, and could not do anything with it even if it did.
相反,您应该执行以下操作:
Instead, you should do something like:
import xml.etree.cElementTree as ET
import urllib2, StringIO
page_with_xml = urllib2.urlopen(r'https://apitest.batchbook.com/api/v1/people.xml?auth_token=GR5doLv88FrnLyLGIwok')
io_xml = StringIO.StringIO()
io_xml.write(page_with_xml.read())
io_xml.seek(0)
tree = ET.parse(io_xml)
经过修改,以更正etree.parse正在寻找类似文件的对象这一事实。虽然不是特别优雅,但是可以完成工作。
Editted to correct for the fact that etree.parse is looking for a file-like object. Not particularly elegant, but it gets the job done.
这篇关于Python ElementTree XML IOError:[Errno 22]无效模式('rb')或文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!