开发者

Python feedparser not using atom/WordPress namespace?

开发者 https://www.devze.com 2023-01-07 20:57 出处:网络
I\'m trying to use feedparser (an excellent library) to parse WordPress export files, and a (minor) inconsistency between WordPress version is causing me a huge headache.

I'm trying to use feedparser (an excellent library) to parse WordPress export files, and a (minor) inconsistency between WordPress version is causing me a huge headache.

WordPress 2.x doesn't include atom:link tags in the XML output (without_atom_tags.xml). When parsed, namespaced elements are available without the prefix:

>>> feed = feedparser.parse("without_atom_tags.xml")
>>> print feed.entries[0].comment_status
u'open'

The XML from WordPress 3.x does contain atom:link tags (with_atom_tags.xml), and you must prefix namespace elements:

>&g开发者_StackOverflow中文版t;> 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'

Interestingly, the prefixes aren't needed if I add xmlns:atom="http://www.w3.org/2005/Atom" to the root RSS element (with_atom_tags_and_namespace.xml).

I need to parse all these different formats without modifying the XML. Is feedparser broken, or am I doing it wrong? Can I do this without a bunch of nasty conditional code?


Could you add the missing namespaces (atom/wp) to the global list of supported namespaces in feedparser.py directly?

0

精彩评论

暂无评论...
验证码 换一张
取 消