Python XML parsing from website

后端 未结 2 1616
余生分开走
余生分开走 2021-02-15 10:04

I am trying to Parse from a website. I am stuck. I will provide the XML below. It is coming from a webiste. I have two questions. What is the best way to read xml from a website

2条回答
  •  猫巷女王i
    2021-02-15 10:45

    Take a look at your code:

    document = ('http://www.newyorkfed.org/markets/omo/dmm/fftoXML.cfm?type=daily''r')
    web = urllib.urlopen(document)
    get_web = web.read()
    xmldoc = minidom.parseString(document)
    

    I'm not sure you have document correct unless you want http://www.newyorkfed.org/markets/omo/dmm/fftoXML.cfm?type=dailyr because that's what you'll get (the parens group in this case and strings listed next to each other automatically concatenate).

    After that you do some work to create get_web but then you don't use it in the next line. Instead you try to parse your document which is the url...

    Beyond that, I would totally suggest you use ElementTree, preferably lxml's ElementTree (http://lxml.de/). Also, lxml's etree parser takes a file-like object which can be a urllib object. If you did, after straightening out the rest of your doc, you could do this:

    from lxml import etree
    from io import StringIO
    import urllib
    
    url = 'http://www.newyorkfed.org/markets/omo/dmm/fftoXML.cfm?type=daily'
    root = etree.parse(urllib.urlopen(url))
    
    for obs in root.xpath('/ff:DataSet/ff:Series/ff:Obs'):
        price = obs.xpath('./base:OBS_VALUE').text
        print(price)
    

提交回复
热议问题