使用ElementTree从XML中提取数据的麻烦

时间:2015-12-11 16:49:56

标签: python xml api rest elementtree

我无法理解如何从以下XML文档中提取数据。

我已通过ECB API下载了一份XML文档。

import urllib.request

access_url = 'https://sdw-wsrest.ecb.europa.eu/service/data/EXR/D.USD.EUR.SP00.A?startPeriod=2000-01-01&endPeriod=2015-12-10'
response = urllib.request.urlretrieve(access_url, 'trial_savename.xml')

检索并保存看起来像这样的XML文档(显示前37行):

<?xml version="1.0" encoding="UTF-8"?><message:GenericData xmlns:message="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message" xmlns:common="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/common" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:generic="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic" xsi:schemaLocation="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message https://sdw-wsrest.ecb.europa.eu:443/vocabulary/sdmx/2_1/SDMXMessage.xsd http://www.sdmx.org/resources/sdmxml/schemas/v2_1/common https://sdw-wsrest.ecb.europa.eu:443/vocabulary/sdmx/2_1/SDMXCommon.xsd http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic https://sdw-wsrest.ecb.europa.eu:443/vocabulary/sdmx/2_1/SDMXDataGeneric.xsd">
<message:Header>
<message:ID>781631bf-c21e-4c88-9657-ae03c858b917</message:ID>
<message:Test>false</message:Test>
<message:Prepared>2015-12-11T16:56:20.723+01:00</message:Prepared>
<message:Sender id="ECB"/>
<message:Structure structureID="ECB_EXR1" dimensionAtObservation="TIME_PERIOD">
<common:Structure>
<URN>urn:sdmx:org.sdmx.infomodel.datastructure.DataStructure=ECB:ECB_EXR1(1.0)</URN>
</common:Structure>
</message:Structure>
</message:Header>
<message:DataSet action="Replace" validFromDate="2015-12-11T16:56:20.723+01:00" structureRef="ECB_EXR1">
<generic:Series>
<generic:SeriesKey>
<generic:Value id="FREQ" value="D"/>
<generic:Value id="CURRENCY" value="USD"/>
<generic:Value id="CURRENCY_DENOM" value="EUR"/>
<generic:Value id="EXR_TYPE" value="SP00"/>
<generic:Value id="EXR_SUFFIX" value="A"/>
</generic:SeriesKey>
<generic:Attributes>
<generic:Value id="SOURCE_AGENCY" value="4F0"/>
<generic:Value id="COLLECTION" value="A"/>
<generic:Value id="DECIMALS" value="4"/>
<generic:Value id="TITLE_COMPL" value="ECB reference exchange rate, US dollar/Euro, 2:15 pm (C.E.T.)"/>
<generic:Value id="UNIT" value="USD"/>
<generic:Value id="TITLE" value="US dollar/Euro"/>
<generic:Value id="UNIT_MULT" value="0"/>
</generic:Attributes>
<generic:Obs>
<generic:ObsDimension value="2000-01-03"/>
<generic:ObsValue value="1.009"/>
<generic:Attributes>
<generic:Value id="OBS_STATUS" value="A"/>
</generic:Attributes>
</generic:Obs>

我想为每个ObsValue value提取ObsDimension value并继续使用这些内容。

我尝试以下列方式使用ElementTree

import xml.etree.ElementTree as ET
tree = ET.parse('trial_savename.xml')
e = tree.findall('message:GenericData')

返回空列表[]。我以为我可以访问像e = tree.findall('message:GenericData/message:DataSet/generic:Series/generic:Obs/generic:ObsDimension value')这样的数据,但似乎不是这样做的。

我出错了什么?

1 个答案:

答案 0 :(得分:2)

您需要传递namespace参数。

>>> import xml.etree.ElementTree as ET
>>> tree = ET.parse('trial_savename.xml')
>>> ns = {'generic': "http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic" }
>>> dimensions = tree.findall('.//generic:ObsDimension', namespaces=ns)
>>> values = [dim.get('value') for dim in dimensions]
>>> values[:5]
['2000-01-03', '2000-01-04', '2000-01-05', '2000-01-06', '2000-01-07']

如果您使用lxml,则可以使用nsmap属性和xpath method

>>> import lxml.etree as ET
>>> tree = ET.parse('trial_savename.xml')
>>> values = tree.xpath('.//generic:ObsDimension/@value', namespaces=tree.getroot().nsmap)
>>> values[:5]
['2000-01-03', '2000-01-04', '2000-01-05', '2000-01-06', '2000-01-07']