如何在 python 中阅读 xml?

How to read xml in python?

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:georss="http://www.georss.org/georss">
<id>tag:blogger.com,1999:blog-5575774992011030841.archive</id>
<updated>2016-12-08T11:17:36.889+05:30</updated>
<title type="text">Rochak News</title>
<link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://www.rochaknews.com/feeds/archive">
<link rel="self" type="application/atom+xml" href="http://www.rochaknews.com/feeds/archive">
<link rel="http://schemas.google.com/g/2005#post" type="application/atom+xml" href="https://www.blogger.com/feeds/5575774992011030841/archive">
<link rel="alternate" type="text/html" href="http://www.rochaknews.com/">
<author>
<generator version="7.00" uri="https://www.blogger.com">Blogger</generator>
<entry>
<id>tag:blogger.com,1999:blog-5575774992011030841.settings.BLOG_BY_POST_ARCHIVING</id>
<published>2016-08-22T19:39:26.425+05:30</published>
<updated>2016-12-08T11:17:36.889+05:30</updated>
<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/blogger/2008/kind#settings"></category>
<title type="text">Whether to provide an archive page for each post</title>
<content type="text">true</content>
<link rel="edit" type="application/atom+xml" href="https://www.blogger.com/feeds/5575774992011030841/settings/BLOG_BY_POST_ARCHIVING">
<link rel="self" type="application/atom+xml" href="https://www.blogger.com/feeds/5575774992011030841/settings/BLOG_BY_POST_ARCHIVING">
<author>
</entry>
<entry></entry>
<entry></entry>
<entry></entry>
<entry>
<id>tag:blogger.com,1999:blog-5575774992011030841.post-1104181070415553676</id>
<published>2016-09-06T13:47:00.006+05:30</published>
<updated>2016-09-06T13:47:55.331+05:30</updated>
<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/blogger/2008/kind#post"></category>
<category scheme="http://www.blogger.com/atom/ns#" term="Bollywood"></category>
<category scheme="http://www.blogger.com/atom/ns#" term="rochaknews"></category>
<title type="text">OMG ! प्रियंका और दीपिका के बाद अब हॉलीवुड जाएगी ये एक्ट्रेस</title>
<content type="html"><div dir="ltr" style="text-align: left;" trbidi="on"><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-vBoYugcMUgc/V857M85aCkI/AAAAAAAAApY/vsIMDYcsy4AiVVxlkavmuG9wWl7mT8n1wCLcB/s1600/Sonam-kapoor-indian-fashion-pictures.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="384" src="https://3.bp.blogspot.com/-vBoYugcMUgc/V857M85aCkI/AAAAAAAAApY/vsIMDYcsy4AiVVxlkavmuG9wWl7mT8n1wCLcB/s640/Sonam-kapoor-indian-fashion-pictures.jpg" width="640" /></a></div><div class="separator" style="clear: both; text-align: center;"></div><br /><br />प्रियंका और दीपिका के बाद अब सोनम कपूर को भी लगा हॉलीवुड का चस्का। &nbsp;बॉलीवुड के बाद अब एक्ट्रेस सोनम कपूर हॉलीवुड में अपनी नयी जर्नी की शुरुआत करने जा रही हैं। स्टाइल डिवा सोनम अब हॉलीवुड फिल्मों में हाथ आज़माने जा रही हैं। सोनम ने इस बात की खबर खुद सोशल मीडिया पर ट्वीट के जरिये दी। उन्होने अपने फैंस को बताया की उन्हें यूनाइटेड टेलेंट एजेंसी ने सभी क्षेत्रों में प्रतिनिधित्व के लिए चुना है।<br /><br />हॉलीवुड में कपूर कोई पहला नाम नहीं है इससे पहले सोनम के पापा भी हॉलीवुड में ‘स्लमडॉग मिलेनियर’ और ‘मिशन इम्पॉसिबल: घोस्ट प्रोटोकॉल’ से अपनी एक्टिंग के जरिये कई दिल जीत चुके हैं। दीपिका पादुकोण और प्रियंका चोपड़ा के बाद अब सोनम भी हॉलीवुड अपने जलवे बिखेरने की तैयारी में हैं। फिलहाल सोनम अपनी नयी फिल्म ‘वीरे दी वैडिंग’ की शूटिंग में बिज़ि हैं।<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-mPIxgYqcXSY/V857TD4J2NI/AAAAAAAAApc/bdPmebhpSMM1L7dPGYDfZtLJ-5Bbt7AswCLcB/s1600/hot%2Bsonam%2Bkapur.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="450" src="https://1.bp.blogspot.com/-mPIxgYqcXSY/V857TD4J2NI/AAAAAAAAApc/bdPmebhpSMM1L7dPGYDfZtLJ-5Bbt7AswCLcB/s640/hot%2Bsonam%2Bkapur.jpg" width="640" /></a></div><br /><a href="http://www.rochaknews.com/2016/09/blog-post_80.html">Read more: ब्रेस्ट इम्प्लांट' के कारण सुर्ख़ियों में आईं ये एक्ट्रेस</a><br /><a href="http://www.rochaknews.com/2016/09/blog-post_22.html">Read more:‘पॉर्न हब’ ने नेत्रहीनों के लिए लॉन्च किया एक खास चैनल</a><br /><a href="http://www.rochaknews.com/2016/08/blog-post_94.html">Read more: अय्याशी के लिए यहाँ मिलते है एक से बढ़ कर एक लड़कियां</a><br /><a href="http://www.rochaknews.com/2016/09/blog-post_69.html">Read more: जानिए, पुरुष बनाना चाहते है ऐसी महिलाओ से सम्बन्ध</a></div></content>
<link rel="replies" type="application/atom+xml" href="http://www.rochaknews.com/feeds/1104181070415553676/comments/default" title="टिप्पणियाँ भेजें">
<link rel="replies" type="text/html" href="http://www.rochaknews.com/2016/09/omg_6.html#comment-form" title="0 टिप्पणियाँ">
<link rel="edit" type="application/atom+xml" href="https://www.blogger.com/feeds/5575774992011030841/posts/default/1104181070415553676">
<link rel="self" type="application/atom+xml" href="https://www.blogger.com/feeds/5575774992011030841/posts/default/1104181070415553676">
<link rel="alternate" type="text/html" href="http://www.rochaknews.com/2016/09/omg_6.html" title="OMG ! प्रियंका और दीपिका के बाद अब हॉलीवुड जाएगी ये एक्ट्रेस">
<author>
<media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://3.bp.blogspot.com/-vBoYugcMUgc/V857M85aCkI/AAAAAAAAApY/vsIMDYcsy4AiVVxlkavmuG9wWl7mT8n1wCLcB/s72-c/Sonam-kapoor-indian-fashion-pictures.jpg" height="72" width="72"></media:thumbnail>
<thr:total>0</thr:total>
</entry>
<entry></entry>

</feed>

我正在阅读 python 中的这个 xml 文件。以上 xml 只是样本,只有一些 content.It 基本上有 <feed> 和 child <entry> 和子 child 类别,例如 <title> ,<published><content type="html">。只有一些 <entry> 标签的内容类型 = html。我只想读取 <entry> 标签,其中有 <content type="html">。我怎样才能阅读它?请建议

我得到 <entry> 及其 child 为:

import xml.etree.ElementTree as etree
tree = etree.parse('xmlread/template/blog-12-08-2016.xml')
entries = tree.findall('{http://www.w3.org/2005/Atom}entry')
print len(entries)



for child in entries:
  title_element = child.find('{http://www.w3.org/2005/Atom}title')
  published = child.find('{http://www.w3.org/2005/Atom}published')
  # print published.text
  # print title_element.text

但无法只读取只有内容类型="html" 的特定<entry> 标签。请调查此事。

我觉得你的 XML 很生气。

xml.etree.ElementTree.ParseError: mismatched tag: line 21, column 2

修复后,请按以下步骤操作:

parent = ET.fromstring(data)
print [child.text for child in parent.findall('.//child[@type="html"]')]

:

import xml.etree.ElementTree as ET

data = """
<parent>
    <child attr="test">1</child>
    <child attr="something else">2</child>
    <child other_attr="other">3</child>
    <child>4</child>
    <child attr="test">5</child>
</parent>
"""

parent = ET.fromstring(data)
print [child.text for child in parent.findall('.//child[@attr]')]
print [child.text for child in parent.findall('.//child[@attr="test"]')]