【发布时间】:2014-04-25 16:50:10
【问题描述】:
使用以下代码:
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),然后执行以下操作:
tree = ET.parse(r'C:\Users\Eric\Downloads\people.xml')
tree.getroot()
我得到结果:
关于为什么使用链接不起作用的任何线索?谢谢:)
【问题讨论】:
-
etree不包含 HTTP 库。它仅适用于字符串或本地文件。您必须使用urllib2或requests下载xml,然后在本地进行解析。
标签: python xml xml-parsing elementtree