Skip to main content
 首页 » 编程设计

python之使用 PyRSS2Gen 支持 PubSubHubbub

2025年05月04日9TianFang

我正在使用 PyRSS2Gen 生成 RSS 提要,我正在尝试支持 PubSubHubbub,但我需要添加破坏 RSS 的链接元素。以下是无需重写为 Atom 即可工作的内容:

<atom:link rel="hub" href="http://example.hub.com" xmlns:atom="http://www.w3.org/2005/Atom"> 
<atom:link rel="self" href="http://example.com" xmlns:atom="http://www.w3.org/2005/Atom"> 

我能以某种方式在 PyRSS2Gen 中添加任意 XML 吗?我认为 element_attrs 或 rss_attrs 不足以完成此任务,是吗? PyRSS2Gen.RSS2() 最多需要 1 个链接元素,那么我该怎么做呢?

谢谢,

金博尔

请您参考如下方法:

我查看了 PyRSS2Gen 源代码,看起来您所要做的就是覆盖 RSS 类的 publish_extensions() 方法。也许是这样的:

import PyRSS2Gen 
 
class MyRSS2(PyRSS2Gen.RSS2): 
    def publish_extensions(self, handler): 
        PyRSS2Gen._element(handler, 'atom:link', None, {'element': 'attributes here'}) 

然后使用 MyRSS2 而不是 PyRSS2Gen.RSS2。这是未经测试的代码,我建议在将其投入生产之前对其进行检查,眨眼。