我正在尝试编写一个Python脚本,它将搜索Shodan API并返回ID、CVE和描述。由于我的一些搜索结果(例如‘java’)没有已建立的CVE (或CVE键),因此我的脚本会卡住。我知道我需要在try/except错误处理中封装搜索,但我在搜索web时没有找到任何幸运的东西。这是我得到的错误,下面是代码。非常提前谢谢你。
-错误
print '%s: %s: %s' % (exploit['id'], exploit['cve'], exploit['description'])
KeyError: 'cve-我的代码
from shodan import WebAPI
api = WebAPI("my shodan key")
user_selection = raw_input("Enter something: ")
print "searching for", (user_selection),"....."
results = api.exploitdb.search(user_selection)
for exploit in results['matches']:
print '%s: %s: %s' % (exploit['id'], exploit['cve'], exploit['description'])发布于 2013-03-08 01:25:48
看起来您想要使用dict.get,并为其提供一个默认值,以便在键不存在的情况下返回:
print '%s: %s: %s' % (exploit.get('id', '[blank]'), exploit.get('cve', '[blank]'), exploit.get('description', '[blank]'))https://stackoverflow.com/questions/15277215
复制相似问题