views:

168

answers:

4
from xml.dom.minidom import parse, parseString

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

print dom

... the above code throws an IOError: 2, 'No such file or directory'. Python doesn't read remote doc like PHP? What do I need to change in the code to make it read the XML file?

Thanks

A: 

Use urllib.urlopen().

Ignacio Vazquez-Abrams
urllib2.urlopen() does the same thing I believe, but it is also forward compatible with v3.0
Roman Stolper
A: 

The urllib.urlopen() method returns a File Object. So you can directly pass it to your parse() method

Amit
+1  A: 

open is used to open a file on a local filesystem. It cannot open URLs. You have to use urllib.urlopen which returns a file like object that supports a subset of the API.

Noufal Ibrahim
+2  A: 

Using urllib2.urlopen():

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

This XML uses a Cube tag for too many constructs, so selecting currencies gets a little heady.

>>> [elem.attributes['currency'].value for elem in
     dom.getElementsByTagName('Cube') if elem.hasAttribute('currency')]
[u'USD', u'JPY', u'BGN', u'CZK', u'DKK', u'EEK', u'GBP', u'HUF', u'LTL', u'LVL',
 u'PLN', u'RON', u'SEK', u'CHF', u'NOK', u'HRK', u'RUB', u'TRY', u'AUD', u'BRL',
 u'CAD', u'CNY', u'HKD', u'IDR', u'INR', u'KRW', u'MXN', u'MYR', u'NZD', u'PHP',
 u'SGD', u'THB', u'ZAR']
>>> 
gimel
Thanks for the list comprehension, helped a lot!
Nimbuz