2017-11-25 234 views
1

我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) 
+0

真正體會到了努力提供一個[MCVE(https://stackoverflow.com/help/mcve)! +1 – har07

回答

0

你沒佔到默認命名空間。沒有前綴的子元素隱含繼承此命名空間即包括你嘗試迭代通過item元素:

ns = {'d': 'http://purl.org/rss/1.0/', 
    ....} 
for item in tree.findall('d:item', ns): 
    ..... 
+0

現在感謝它的工作 – novski