python etree.ElementTree解析复杂的命名空间

efzxgjgh  于 2023-05-02  发布在  Python
关注(0)|答案(1)|浏览(105)

实际上我必须解析一个online resource,但我试图通过复制一个。xml文件复制到mywww.example所在的目录 www.example.com 。在这个示例中,我将文件缩减为3个项目和2种货币,并为简单起见将其解析为字符串。

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 currencies -->
      </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。我实际上做的是在最后,只有几行:

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)

现在转到终端并执行:python3www.example www.example.com 。..
我想得到日元的货币(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)
aor9mmx1

aor9mmx11#

您没有考虑到 *default命名空间 *。不带前缀的后代元素隐式继承此命名空间i。e包括您尝试迭代的item元素:

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

相关问题