在Python中打开远程文档

2024-04-26 18:25:25 发布

您现在位置:Python中文网/ 问答频道 /正文

from xml.dom.minidom import parse, parseString

datasource = open('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml')
dom = parse(datasource)

print dom

。。。上面的代码抛出一个IOError: 2, 'No such file or directory'。Python不像PHP那样读取远程文档?我需要在代码中做些什么来让它读取XML文件?在

谢谢


Tags: 代码fromimporthttpparsewwwxmlopen
3条回答

open用于打开本地文件系统上的文件。它无法打开URL。您必须使用urllib.urlopen,它返回一个支持API子集的类似文件的对象。在

使用^{}

>>> import urllib2
>>> from xml.dom.minidom import parse, parseString
>>> u1=urllib2.urlopen('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml')
>>> dom=parse(u1)
>>> print dom
<xml.dom.minidom.Document instance at 0x017D73A0>
>>> dom.childNodes
[<DOM Element: gesmes:Envelope at 0x17d7c88>]
>>> dom.childNodes[0].childNodes
[<DOM Text node "u'\n\t'">, <DOM Element: gesmes:subject at 0x1041aa8>,
 <DOM Text node "u'\n\t'">, <DOM Element: gesmes:Sender at 0xff8260>,
 <DOM Text node "u'\n\t'">, <DOM Element: Cube at 0x17d3dc8>, <DOM Text node "u'\n'">]
>>> 

这个XML使用了一个Cube标记来处理太多的构造,所以选择货币有点令人兴奋。在

^{pr2}$

使用^{}。在

相关问题 更多 >