【问题标题】:Feedparser not parsing searched for descriptionFeedparser 未解析搜索描述
【发布时间】: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


【解决方案1】:

这有帮助。我不知道合取逻辑,但已经解决了。我省略了 contains_wanted(title.lower()) 因为这在语句逻辑中不是必需的,因为 contains_wanted(description.lower()) 满足了标题语句的目的以及它自己的目的。并得到正确的输出。

谢谢你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 2015-12-19
    相关资源
    最近更新 更多