如何检测RSS提要中的已更改项目和新项目?

时间:2009-03-31 08:39:38

标签: python rss feeds

使用feedparser或其他一些Python库下载和解析RSS提要;如何可靠地检测new项和modified项?

到目前为止,我已经看到Feed中的新项目的发布日期早于最新项目。此外,我看到饲料阅读器显示相同的项目发布时内容略有不同作为单独的项目。我没有实现提要阅读器应用程序,我只想要一个合理的策略来存档提要数据。

2 个答案:

答案 0 :(得分:5)

这取决于您对饲料来源的信任程度。 feedparser为feed项提供.id属性 - 对于RSS和ATOM源,此属性应该是唯一的。有关示例,请参阅例如feedparser的ATOM docs。尽管.id将涵盖大多数情况,但可以想象源可能会发布具有相同ID的多个项目。在这种情况下,您没有太多选择,只能散列项目的内容。

答案 1 :(得分:1)

HTTP Features中有两个documentation for feedparser可以完成此任务:

1。使用ETag减少带宽

基本概念是,提要发布者在发布提要时可以提供一个特殊的HTTP标头,称为ETag。您应该在后续请求中将此ETag发送回服务器。如果自上次请求以来,供稿未更改,则服务器将返回特殊的HTTP状态代码(304),并且不包含供稿数据。

    import feedparser
    d = feedparser.parse('` <http://feedparser.org/docs/examples/atom10.xml>`_')
    d.etag``'"6c132-941-ad7e3080"'``
    d2 = feedparser.parse('` <http://feedparser.org/docs/examples/atom10.xml>`_', etag=d.etag)
    d2.status``304``
    d2.feed``{}``
    d2.entries``[]``
    d2.debug_message``'The feed has not changed since you last checked, so
    the server sent no data.  This is a feature, not a bug!'

2。使用Last-Modified标头减少带宽

在这种情况下,服务器在HTTP标头中发布提要的最后修改日期。您可以根据后续请求将此内容发送回服务器,如果供稿未更改,则服务器将返回HTTP状态代码304,并且不包含任何供稿数据。

import feedparser
d = feedparser.parse('` <http://feedparser.org/docs/examples/atom10.xml>`_')
d.modified``(2004, 6, 11, 23, 0, 34, 4, 163, 0)``
d2 = feedparser.parse('` <http://feedparser.org/docs/examples/atom10.xml>`_', modified=d.modified)
d2.status``304``
d2.feed``{}``
d2.entries``[]``
d2.debug_message``'The feed has not changed since you last checked, so
the server sent no data.  This is a feature, not a bug!'