python etree.ElementTree用dificult命名空间解析
我acualy必须解析online resource,但试图通过将.xml文件复制到我有我的currency.py文件的目录来简化它。然后从manual here开始,这很短暂,因为我眼中有那么复杂的东西。 在这个示例中,我将该文件分割为3个项目和2个currencys,并将其解析为字符串以简化操作。python etree.ElementTree用dificult命名空间解析
import xml.etree.ElementTree as ET #tree = ET.ElementTree(file='currency.xml')
data = '''
<rdf:RDF xmlns:cb="http://www.cbwiki.net/wiki/index.php/Specification_1.2/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/">
<channel rdf:about="https://www.snb.ch/de/ifor/media/id/media_rss">
<title>SNB Devisenkurse</title>
<link>https://www.snb.ch/de/ifor/media/id/media_rss</link>
<description>Schweizerische Nationalbank (SNB): Devisenkurse (Ankauf Zürich 11 Uhr)</description>
<items>
<rdf:Seq>
<rdf:li rdf:resource="https://www.snb.ch#GBP_4782176f7871403c002581de004629c7"/>
<rdf:li rdf:resource="https://www.snb.ch#JPY_4782176f7871403c002581de004629c7"/>
<rdf:li rdf:resource="https://www.snb.ch#EUR_4782176f7871403c002581de004629c7"/>
<!-- here are more currencys... -->
</rdf:Seq>
</items>
<dc:publisher>SNB</dc:publisher>
<dc:rights>Copyright © Schweizerische Nationalbank, Zürich (Schweiz) 2017</dc:rights>
<dcterms:license>https://www.snb.ch/de/srv/id/disclaimer</dcterms:license>
<dcterms:created>2017-11-20T19:30:01+01:00</dcterms:created>
</channel>
<item rdf:about="https://www.snb.ch#GBP_4782176f7871403c002581de004629c7">
<title>CH: 1.3116 CHF = 1 GBP 2017-11-20 Tägliche Kurse (11:00)</title>
<link>https://www.snb.ch</link>
<description>1 GBP = 1.3116 CHF (Tägliche Kurse (11:00); 2017-11-20T13:46:22.357+01:00)</description>
<dc:date>2017-11-20T13:46:22.357+01:00</dc:date>
<dc:language>de</dc:language>
<cb:statistics rdf:parseType="Resource">
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#Statistics"/>
<cb:country>CH</cb:country>
<cb:institutionAbbrev>SNB</cb:institutionAbbrev>
<cb:exchangeRate rdf:parseType="Resource">
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#ExchangeRate"/>
<cb:observation rdf:parseType="Resource">
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#Observation"/>
<cb:value>1.3116</cb:value>
<cb:unit>CHF</cb:unit>
<cb:decimals>4</cb:decimals>
</cb:observation>
<cb:baseCurrency>CHF</cb:baseCurrency>
<cb:targetCurrency>GBP</cb:targetCurrency>
<cb:rateType>Tägliche Kurse (11:00)</cb:rateType>
<cb:observationPeriod rdf:parseType="Resource">
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#ObservationPeriod"/>
<cb:frequency>daily</cb:frequency>
<cb:period>2017-11-20</cb:period>
</cb:observationPeriod>
</cb:exchangeRate>
</cb:statistics>
</item>
<item rdf:about="https://www.snb.ch#JPY_4782176f7871403c002581de004629c7">
<title>CH: 0.8819 CHF = 100 JPY 2017-11-20 Tägliche Kurse (11:00)</title>
<link>https://www.snb.ch</link>
<description>100 JPY = 0.8819 CHF (Tägliche Kurse (11:00); 2017-11-20T13:46:22.350+01:00)</description>
<dc:date>2017-11-20T13:46:22.350+01:00</dc:date>
<dc:language>de</dc:language>
<cb:statistics rdf:parseType="Resource">
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#Statistics"/>
<cb:country>CH</cb:country>
<cb:institutionAbbrev>SNB</cb:institutionAbbrev>
<cb:exchangeRate rdf:parseType="Resource">
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#ExchangeRate"/>
<cb:observation rdf:parseType="Resource">
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#Observation"/>
<cb:value>0.8819</cb:value>
<cb:unit>CHF</cb:unit>
<cb:unit_mult>-2</cb:unit_mult>
<cb:decimals>4</cb:decimals>
</cb:observation>
<cb:baseCurrency>CHF</cb:baseCurrency>
<cb:targetCurrency>JPY</cb:targetCurrency>
<cb:rateType>Tägliche Kurse (11:00)</cb:rateType>
<cb:observationPeriod rdf:parseType="Resource">
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#ObservationPeriod"/>
<cb:frequency>daily</cb:frequency>
<cb:period>2017-11-20</cb:period>
</cb:observationPeriod>
</cb:exchangeRate>
</cb:statistics>
</item>
<item rdf:about="https://www.snb.ch#EUR_4782176f7871403c002581de004629c7">
<title>CH: 1.1665 CHF = 1 EUR 2017-11-20 Tägliche Kurse (11:00)</title>
<link>https://www.snb.ch</link>
<description>1 EUR = 1.1665 CHF (Tägliche Kurse (11:00); 2017-11-20T13:46:22.343+01:00)</description>
<dc:date>2017-11-20T13:46:22.343+01:00</dc:date>
<dc:language>de</dc:language>
<cb:statistics rdf:parseType="Resource">
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#Statistics"/>
<cb:country>CH</cb:country>
<cb:institutionAbbrev>SNB</cb:institutionAbbrev>
<cb:exchangeRate rdf:parseType="Resource">
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#ExchangeRate"/>
<cb:observation rdf:parseType="Resource">
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#Observation"/>
<cb:value>1.1665</cb:value>
<cb:unit>CHF</cb:unit>
<cb:decimals>4</cb:decimals>
</cb:observation>
<cb:baseCurrency>CHF</cb:baseCurrency>
<cb:targetCurrency>EUR</cb:targetCurrency>
<cb:rateType>Tägliche Kurse (11:00)</cb:rateType>
<cb:observationPeriod rdf:parseType="Resource">
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#ObservationPeriod"/>
<cb:frequency>daily</cb:frequency>
<cb:period>2017-11-20</cb:period>
</cb:observationPeriod>
</cb:exchangeRate>
</cb:statistics>
</item>
<!-- here are more currencys... -->
</rdf:RDF>'''
tree = ET.fromstring(data)
ns = {'rdf':'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'cb': 'http://www.cbwiki.net/wiki/index.php/Specification_1.2/'}
for item in tree.findall('item'):
for stat in item.findall('cb:statistics', ns):
for exrate in stat.findall('cb:exchangeRate', ns):
currency = exrate.find('cb:targetCurrency', ns).text
print(currency)
所以,那只是一个MWE。我actualy做的是中端和短短的几行:
tree = ET.fromstring(data) ns = {'rdf':'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'cb': 'http://www.cbwiki.net/wiki/index.php/Specification_1.2/'}
for item in tree.findall('item'):
for stat in item.findall('cb:statistics', ns):
for exrate in stat.findall('cb:exchangeRate', ns):
currency = exrate.find('cb:targetCurrency', ns).text
print(currency)
现在要终端和executeing: python3 currency.py 什么也没有发生......
我沃尔德想获得货币JPY(cb:targetCurrency),并将其乘以10 x(cb:unit_mult)字段。但现在我无法得到节点的任何文本... 我怎样才能得到货币?
解决方案:
import xml.etree.ElementTree as ET # terminal: cd to this .py file and then do
# $wget https://www.snb.ch/selector/de/mmr/exfeed/rss
# rename the file to rss.xml
tree = ET.ElementTree(file='rss.xml')
root = tree.getroot()
ns = {'rdf':'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'none':'http://purl.org/rss/1.0/',
'dc':'http://purl.org/dc/elements/1.1/',
'dcterms':'http://purl.org/dc/terms/',
'cb': 'http://www.cbwiki.net/wiki/index.php/Specification_1.2/'}
for item in root.findall('none:item',ns):
for statistics in item.findall('cb:statistics',ns):
for exchangerate in statistics.findall('cb:exchangeRate',ns):
for targetcurrency in exchangerate.findall('cb:targetCurrency',ns):
print(targetcurrency.text)
回答:
你没占到默认命名空间。没有前缀的子元素隐含继承此命名空间即包括你尝试迭代通过item
元素:
ns = {'d': 'http://purl.org/rss/1.0/', ....}
for item in tree.findall('d:item', ns):
.....
以上是 python etree.ElementTree用dificult命名空间解析 的全部内容, 来源链接: utcz.com/qa/257132.html