简体   繁体   English

如何在 Python 中通过 ElementTree 使用 RDF 解析 XML 文档?

[英]How to parse XML doc with RDF by ElementTree in Python?

I have xml like :我有像这样的 xml:

<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/">
 <channel rdf:about="https://pracujwit.pl/rss/all/">
  <description>Najnowsze oferty</description>
  <link>https://pracujwit.pl/</link>
  <title>Pracuj w IT</title>
  <dc:date>05-02-2020</dc:date>
  <items>
   <rdf:Seq>
    <rdf:li rdf:resource="https://pracujwit.pl/job/192829/bi-consultant-at-primaris/"/>
    <rdf:li rdf:resource="https://pracujwit.pl/job/192827/senior-python-developer-100-zdalnie-at-newperspective/"/>
    <rdf:li rdf:resource="https://pracujwit.pl/job/192826/kierownik-projektu-it-at-comarch-sa/"/>
   </rdf:Seq>
  </items>
 </channel>
 <item rdf:about="https://pracujwit.pl/job/192829/bi-consultant-at-primaris/">
  <description><![CDATA[<strong>Lokalizacja:</strong> Warszawa<br /><strong>Firma:</strong> Primaris<br /><strong>Oferta:</strong><br /><br /><br /><a href="https://pracujwit.pl/job/192829//">Aplikuj online</a><br />]]></description>
  <link>https://pracujwit.pl/job/192829/bi-consultant-at-primaris/</link>
  <title><![CDATA[BI Consultant]]></title>
  <company><![CDATA[Primaris]]></company>
  <location><![CDATA[Warszawa]]></location>
  <dc:date>2020-02-04 15:12:32</dc:date>
 </item>
 <item rdf:about="https://pracujwit.pl/job/192827/senior-python-developer-100-zdalnie-at-newperspective/">
  <description><![CDATA[<strong>Lokalizacja:</strong> <br /><strong>Firma:</strong> NewPerspective <br /><strong>Oferta:</strong><br /><br /><br /><a href="https://pracujwit.pl/job/192827//">Aplikuj online</a><br />]]></description>
  <link>https://pracujwit.pl/job/192827/senior-python-developer-100-zdalnie-at-newperspective/</link>
  <title><![CDATA[Senior Python Developer / 100% zdalnie]]></title>
  <company><![CDATA[NewPerspective ]]></company>
  <location><![CDATA[]]></location>
  <dc:date>2020-02-04 11:45:34</dc:date>
 </item>
 <item rdf:about="https://pracujwit.pl/job/192826/kierownik-projektu-it-at-comarch-sa/">
  <description><![CDATA[<strong>Lokalizacja:</strong> Kraków<br /><strong>Firma:</strong> Comarch SA<br /><strong>Oferta:</strong><br /><br /><br /><a href="https://pracujwit.pl/job/192826//">Aplikuj online</a><br />]]></description>
  <link>https://pracujwit.pl/job/192826/kierownik-projektu-it-at-comarch-sa/</link>
  <title><![CDATA[Kierownik Projektu IT]]></title>
  <company><![CDATA[Comarch SA]]></company>
  <location><![CDATA[Kraków]]></location>
  <dc:date>2020-02-04 09:33:05</dc:date>
 </item>
</rdf:RDF>

I save it to file 'xml_rdf.txt'.我将它保存到文件“xml_rdf.txt”中。 I usually coding parsers to XML like this:我通常像这样将解析器编码为 XML:

import xml.etree.ElementTree as ET

path = 'path/to/xml_rdf.txt'
xml_tree = ET.parse(path/to/xml_rdf.txt)

for item in xml_tree.iter('item'):
    print(item)

But in this case i don't get any items.但在这种情况下,我没有得到任何物品。 I know about specify namespaces on XML parsers, but I have problem with this in this case.我知道在 XML 解析器上指定命名空间,但在这种情况下我有这个问题。 I try sth like :我尝试诸如:

ns = {"dcterms": "http://purl.org/rss/1.0/"}
for item in xml_tree.iter('dcterms:item'):
    print(item)

But the same story, no items.但同样的故事,没有项目。

Anybody have idea how deal with it?有人知道如何处理吗?

With iter() , you must use the namespace URI:使用iter() ,您必须使用命名空间 URI:

for item in xml_tree.iter('{http://purl.org/rss/1.0/}item'):
    print(item)

Output:输出:

<Element '{http://purl.org/rss/1.0/}item' at 0x7f6ff8d5ad90>
<Element '{http://purl.org/rss/1.0/}item' at 0x7f6ff8d5af50>
<Element '{http://purl.org/rss/1.0/}item' at 0x7f6ff8d64150>

With findall() , you can use a prefix:使用findall() ,您可以使用前缀:

ns = {"dcterms": "http://purl.org/rss/1.0/"}

for item in xml_tree.findall('dcterms:item', ns):
    print(item)

Thanks for help @mzjn.感谢@mzjn 的帮助。 Finally I get items and their data in this way:最后,我以这种方式获取项目及其数据:

namespaces = {'xml_root': 'http://purl.org/rss/1.0/',
              'xml_root_dc': 'http://purl.org/dc/elements/1.1/'}

for offer in xml_tree.findall('./xml_root:item', namespaces):
    url = offer.find('./xml_root:link', namespaces).text
    date_publication = offer.find('./xml_root_dc:date', namespaces).text

Topic to close.要关闭的主题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM