【发布时间】:2019-06-28 12:57:17
【问题描述】:
我正在尝试利用 RSS 来获取有关我可能关心的特定安全漏洞的自动通知。我已经获得了在提要条目的标题和 url 中搜索关键字的功能,但它似乎忽略了 rss 描述。
我已验证提要中存在描述字段(在发现此内容之前,我最初是从摘要开始而不是描述),但不明白为什么它不起作用(对于 python 来说相对较新)。这可能是一个卫生问题,还是我遗漏了一些关于如何执行搜索的内容?
#!/usr/bin/env python3.6
import feedparser
#Keywords to search for in the rss feed
key_words = ['Chrome','Tomcat','linux','windows']
# get the urls we have seen prior
f = open('viewed_urls.txt', 'r')
urls = f.readlines()
urls = [url.rstrip() for url in urls]
f.close()
#Returns true if keyword is in string
def contains_wanted(in_str):
for wrd in key_words:
if wrd.lower() in in_str:
return True
return False
#Returns true if url result has not been seen before
def url_is_new(urlstr):
# returns true if the url string does not exist
# in the list of strings extracted from the text file
if urlstr in urls:
return False
else:
return True
#actual parsing phase
feed = feedparser.parse('https://nvd.nist.gov/feeds/xml/cve/misc/nvd-rss.xml')
for key in feed["entries"]:
title = key['title']
url = key['links'][0]['href']
description = key['description']
#formats and outputs the specified rss fields
if contains_wanted(title.lower()) and contains_wanted(description.lower()) and url_is_new(url):
print('{} - {} - {}\n'.format(title, url, description))
#appends reoccurring rss feeds in the viewed_urls file
with open('viewed_urls.txt', 'a') as f:
f.write('{}\n'.format(title,url))
【问题讨论】:
-
contains_wanted(title.lower()) and contains_wanted(description.lower()) and url_is_new(url)你确定这应该是连词吗? -
因为如果标题不包含想要的单词,那么这个 if 语句中的其他表达式不会被评估。
标签: python rss feedparser