Python - 如何搜索字典键以查看它是否包含某个字符串

时间:2018-06-13 18:48:13

标签: python rss

我试图尽我所能地说出我的问题,因为我不确定这是不是我正在寻找的,对不起提前抱歉。

我正在使用feedparser解析RSS源。

根据文档,我能够将我的结果解析为指向某个漏洞+ RSS帖子标题的链接。

这是我的代码:

import feedparser

# product variables
ie = ['Internet Explorer', 'IE', 'Explorer', 'Internet_Explorer']
nix = ['Linux', 'SUSE', 'Red Hat', 'RHEL', 'Ubuntu', 'Debian_Linux']
win = ['Windows_']

# NVD RSS feed variable
d = feedparser.parse('https://nvd.nist.gov/feeds/xml/cve/misc/nvd-rss-  analyzed.xml')
print("================================")
print(d['feed']['title'])
print("================================")

for entry in d.entries:
    print(entry.title, entry.link)

这将返回如下所示的结果:

CVE-2018-8132 (windows_10, windows_server_2016) https://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2018-8132

我还试过解析原始数据,即:

for entry in d.entries:
    print(entry)

现在这告诉我title是字典中的键(我认为),值为CVE-2018-8132 (windows_10, windows_server_2016)

我想要做的是,if语句是这样的:如果titlevalue包含一个单词(从顶部列表中)然后执行某些操作

我无法在feedparser的文档中找到如何执行此操作。我真的很感激任何帮助。

编辑(2018-06-15):

我明白了。

以下是我未来寻找它的人的代码:

# library imports
import feedparser
import re
import smtplib
import datetime
import time

# NVD RSS feed variable
d = feedparser.parse('https://nvd.nist.gov/feeds/xml/cve/misc/nvd-rss-  analyzed.xml')
print("\n+++++++++++++++++++++++++++++++++++++++")
print(d['feed']['title'], 'Scanner')
print("+++++++++++++++++++++++++++++++++++++++\n")


# function for iterating through the entries and printing out how many vulns there are
def product_scan(product_name):

    # vulnerability links list
    vuln_list = []

    # counter for how many vulns per product
    count = 0
    for entry in d.entries:
        if product_name in entry.title:
            count += 1
            # here we append the hyperlinks of the CVEs to a pre-defined list so we can manipulate it later
            vuln_list.append(entry.link)


    # making it look nice
    if count == 1:
 print('===============================================================\nThere is', count, product_name,
          'related vulnerability:')
    elif count == 0:
        print('')
    else:
      print('===============================================================\nThere are', count, product_name,
          'related vulnerabilities:')

    # this for loop is for enumerating the links for each product CVE code
    for x in vuln_list:
    print(x)


# calling the function and searching for vulns based on keyword(s)
product_list = ['mysql', 'windows', 'linux', 'explorer', 'php', 'webex', 'firefox', 'norton', 'mcafee', 'symantec']
for product in product_list:
    product_scan(product)

1 个答案:

答案 0 :(得分:0)

Geronimo,如果你只想检查字符中是否有密钥,可以使用in关键字

d = {'abc':'123','bbg':'456','tig':'567'}
if 'abc' in d:
   print("key is in here")

如果您想使用过滤后的密钥和&amp ;;值,你可以使用字典理解创建一个单独的字典(类似于列表理解,但对于词典)Filter dict to contain only certain keys?