从rss feed获取新项目

问题描述 投票:0回答:1

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

  1. 有没有其他/更好的方法来查看rss feed是否已更新
  2. 如何创建哈希?仅仅执行以下操作就足够了 import hashlib hash_object = hashlib.sha256(<FEEDPARSER_RESPONSE>) hex_dig = hash_object.hexdigest()
  3. 将hex_dig存储在数据库中
python-3.x rss feedparser hashlib hash-function
1个回答
1
投票

散列FEEDPARSER_RESPONSE是合理的,特别是如果您的Feed中不存在etag或修改后的值。您没有提供RSS源的链接,因此我使用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
© www.soinside.com 2019 - 2024. All rights reserved.