Python XML parsing from website -


i trying parse website. stuck. provide xml below. coming webiste. have 2 questions. best way read xml website, , having trouble digging xml rate need.

the figure need base:obs_value 0.12

what have far:

from xml.dom import minidom import urllib   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)  ff_dataset = xmldoc.getelementsbytagname('ff:dataset')[0]  ff_series = ff_dataset.getelementsbytagname('ff:series')[0]  line in ff_series:     price = line.getelementsbytagname('base:obs_value')[0].firstchild.data     print(price) 

xml code webiste:

-<header> <id>ffd</id>  <test>false</test>   <name xml:lang="en">federal funds daily averages</name> <prepared>2013-05-08</prepared>  <sender id="frbny"> <name xml:lang="en">federal reserve bank of new york</name>  <contact>    <name xml:lang="en">public information web team</name> <email>ny.piwebteam@ny.frb.org</email>   </contact>  </sender>  <!--reportingbegin></reportingbegin--> </header>  <ff:dataset> -<ff:series time_format="p1d" disclaimer="g" ff_method="d" decimals="2" availability="a">  <ffbase:key>  <base:freq>d</base:freq>  <base:rate>ff</base:rate> <base:maturity>o</base:maturity>  <ffbase:ff_scope>d</ffbase:ff_scope>  </ffbase:key>  <ff:obs obs_conf="f" obs_status="a"> <base:time_period>2013-05-07</base:time_period> <base:obs_value>0.12</base:obs_value> 

if wanted stick 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(xml_str).read() xmldoc = minidom.parsestring(xml_str)  obs_values = xmldoc.getelementsbytagname('base:obs_value') # prints first base:obs_value finds print obs_values[0].firstchild.nodevalue  # prints second base:obs_value finds print obs_values[1].firstchild.nodevalue  # prints base:obs_value in xml doc obs_val in obs_values:     print obs_val.firstchild.nodevalue 

however, if want use lxml, use underrun's solution. also, original code had errors. attempting parse document variable, web address. needed parse xml returned website, in example get_web variable.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -