Python feedparser不使用atom / WordPress命名空间?

时间:2010-07-14 19:37:09

标签: python xml wordpress feedparser atom-feed

我正在尝试使用feedparser(一个优秀的库)来解析WordPress导出文件,而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坏了,还是我做错了?我可以在没有一堆讨厌的条件代码的情况下做到这一点吗?

1 个答案:

答案 0 :(得分:0)

您是否可以将缺少的命名空间(atom / wp)直接添加到feedparser.py中支持的命名空间的全局列表中?