我試圖使用feedparser(一個出色的庫)來解析WordPress導出文件,並且WordPress版本之間(小)不一致導致我非常頭疼。Python feedparser不使用atom/WordPress命名空間?
WordPress 2.x在XML輸出中不包含atom:link
標籤(without_atom_tags.xml)。當解析,命名空間的元素沒有前綴可供選擇:
>>> feed = feedparser.parse("without_atom_tags.xml")
>>> print feed.entries[0].comment_status
u'open'
從WordPress的3.x中的XML 不包含atom:link
標籤(with_atom_tags.xml),你必須前綴命名空間的元素:
>>> feed = feedparser.parse("with_atom_tags.xml")
>>> feed.entries[0].wp_comment_status # <-- Note wp_ prefix
u'open'
>>> feed.entries[0].comment_status
AttributeError: object has no attribute 'comment_status'
有趣,如果我將xmlns:atom="http://www.w3.org/2005/Atom"
添加到根RSS元素(with_atom_tags_and_namespace.xml),則不需要前綴。
我需要解析所有這些不同的格式而不修改XML。飼料分解器是否損壞,或者我做錯了嗎?我可以做到這一點沒有一堆討厭的條件代碼?
*更新:*使用feedparser的trunk版本會導致所有元素被解析爲相同(*帶* wp_前綴)。 – 2010-07-14 21:06:31