从 rss 提要获取新项目

Get new items from rss feed

我正在使用 python feedparser 来解析一些 rss 提要(每 2 小时一次),不幸的是,rss 提要不包含 etag 或修改的值,因此每当我解析提要时,我每次都会得到整个数据。我正在考虑创建从 feedparser.parse 返回的条目的散列并将其存储在数据库中,以便下次我再次解析时我可以与散列进行比较并查看提要是否已更改,然后才开始解析提要中的每个项目 我的问题

  1. 有什么other/better方法可以查看 rss 提要是否已更新
  2. 如何创建散列?只做以下就够了吗

    import hashlib 
    hash_object = hashlib.sha256(<FEEDPARSER_RESPONSE>)
    hex_dig = hash_object.hexdigest() 
    
  3. 将hex_dig存入数据库

散列 FEEDPARSER_RESPONSE 是合理的,尤其是当您的 Feed 中不存在 etag 或修改后的值时。您没有为您的 RSS 提要提供 link,所以我使用 CNN 的一个作为我的回答。

import hashlib
import feedparser

cnn_top_news = feedparser.parse('http://rss.cnn.com/rss/cnn_topstories.rss')

# I using entries, because in testing it gave me the same hash.
news_updated = cnn_top_news.entries

###################################################################
# During testing all of these items worked for creating the hash.
# So there are multiple options to choice from.   
#
# cnn_top_news['entries']
# titles = [entry.title for entry in cnn_top_news['entries']]
# summaries = [entry.summary for entry in cnn_top_news['entries']]
###################################################################

hash_object = hashlib.sha256(str(news_updated).encode('utf-8'))
hex_dig = hash_object.hexdigest()

print (hex_dig)
# output 
371c5730c7f1407878a32a814bc72542b48a43e1f7670eae0627d2617289161b