开发者

Opening remote documents in Python

from xml.dom.minidom import parse, parseString

datasource = open('http://www.ecb.europa.eu/stats/eurofxr开发者_如何转开发ef/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


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


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.


Use urllib.urlopen().


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


    import urllib.request
    from xml.dom import minidom

    response = urllib.request.urlopen('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml')
    results = response.read()

    #print(results)     


    xmldoc = minidom.parseString(results)
    itemlist = xmldoc.getElementsByTagName('Cube') 
    # if itemlist[1].hasAttribute('time'): 
    #     print("Yes it has")
    # for s in itemlist[2].attributes.values():
    #     print(s.value)     
    #     print(itemlist[2].attributes.values())

    for s in itemlist:
        if s.hasAttribute('currency'):
            print(s.attributes['currency'].value + ' ' + s.attributes['rate'].value)
        else:
            pass
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜