Python XML parsing from website

后端 未结 2 1605
余生分开走
余生分开走 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条回答
  •  别那么骄傲
    2021-02-15 10:52

    If you wanted to stick with xml.dom.minidom, try this...

    from xml.dom import minidom
    import urllib
    
    url_str = 'http://www.newyorkfed.org/markets/omo/dmm/fftoXML.cfm?type=daily'
    xml_str = urllib.urlopen(url_str).read()
    xmldoc = minidom.parseString(xml_str)
    
    obs_values = xmldoc.getElementsByTagName('base:OBS_VALUE')
    # prints the first base:OBS_VALUE it finds
    print obs_values[0].firstChild.nodeValue
    
    # prints the second base:OBS_VALUE it finds
    print obs_values[1].firstChild.nodeValue
    
    # prints all base:OBS_VALUE in the XML document
    for obs_val in obs_values:
        print obs_val.firstChild.nodeValue
    

    However, if you want to use lxml, use underrun's solution. Also, your original code had some errors. You were actually attempting to parse the document variable, which was the web address. You needed to parse the xml returned from the website, which in your example is the get_web variable.

提交回复
热议问题