2013-05-08 185 views
8

我想解析一个网站。我被卡住了。我将在下面提供XML。它来自网站。我有两个问题。什么是从网站上读取XML的最佳方式,然后我无法挖掘到XML来获得我需要的速度。从网站解析Python XML

我需要后面的数字为基础:OBS_VALUE 0.12

我到目前为止有:

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] 

for line in ff_series: 
    price = line.getElementsByTagName('base:OBS_VALUE')[0].firstChild.data 
    print(price) 

XML代码网站:

-<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>[email protected]</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> 

回答

7

如果你想坚持xml.dom.minidom,试试这个...

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 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 doc 
for obs_val in obs_values: 
    print obs_val.firstChild.nodeValue 

然而,如果你想使用lxml,请使用underrun的解决方案。此外,您的原始代码有一些错误。你实际上试图解析文档变量,这是网址。你需要解析从网站返回的xml,在你的例子中是get_web变量。

+0

谢谢。我需要使用minidom。感谢您的更正。 – 2013-05-08 13:58:39

+0

添加的信息赞赏 – 2013-05-08 14:01:58

+0

为什么您将url_str更改为xml_str? 应该是: xml_str = urllib.urlopen(url_str).read() – Moulde 2016-01-30 18:35:17

3

看看你的代码:

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) 

我不确定你的文档是否正确,除非你想要http://www.newyorkfed.org/markets/omo/dmm/fftoXML.cfm?type=dailyr,因为这就是你会得到的(在这种情况下,parens组和串联列出的字符串彼此自动连接)。

之后,你做了一些工作来创建get_web,但是你不在下一行使用它。相反,你试图解析你的document这是网址...

除此之外,我会完全建议你使用ElementTree,最好是lxml的ElementTree(http://lxml.de/)。另外,lxml的etree解析器还有一个类似文件的对象,它可以是一个urllib对象。如果你没有,理顺您的文档后剩下的,你可以这样做:

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)