简介:
文章基于pythonBeautifulSoup 爬虫模块爬取绿盟科技漏洞扫描器NOSFUCS的htmll漏洞报告,整理有用数据于excel表上
文末给出扫描器输出报告的html前端页面以及网页源代码,助于分析脚本代码。
collections是Python内建的一个集合模块,提供了许多有用的集合类。
使用dict
时,Key是无序的。在对dict
做迭代时,我们无法确定Key的顺序。如果要保持Key的顺序,可以用OrderedDict
:
https://www.cnblogs.com/frydsh/archive/2012/07/10/2585370.html
zip函数接受任意多个(包括0个和1个)序列作为参数,返回一个tuple列表。
Python3.5中,open()不同模式如r、r+ 、w+、w、a、a+有何不同?
r 只能读
r+ 可读可写 不会创建不存在的文件 从顶部开始写 会覆盖之前此位置的内容
w+ 可读可写 如果文件存在 则覆盖整个文件不存在则创建
w 只能写 覆盖整个文件 不存在则创建
a 只能写 从文件底部添加内容 不存在则创建
a+ 可读可写 从文件顶部读取内容 从文件底部添加内容 不存在则创建
# author LDF
# date 2018-02-14
try:
from bs4 import BeautifulSoup
except:
print('pip3 install beautifulsoup4')
from collections import OrderedDict
import re, os
columns_head = ('漏洞等级', '主机IP', '端口', '协议', '服务', '漏洞名称')
columns_tail = ('详细描述', '解决办法')
columns_tail_default_list = tuple(zip(columns_tail, [''] * len(columns_tail))) #构成有2元素,方便后续做字典?
ip_re = re.compile(r'(?s)IP地址</th>(?:.*?)<td>(.*?)</td>')
def scan_vulns(detail_table):
clear_re = re.compile(r'\S+') # 匹配非空白行
vulns_detail_map = dict()
for tr in detail_table.findAll('tr', {'class':"solution"}):
table_id = tr.get('id')
infos = OrderedDict(columns_tail_default_list)
for item in tr.findAll('tr'):
key = item.th.get_text()
value = item.td.get_text().strip().replace(',', ',') #去除首尾空格,英文','改中文','
if key in infos:
if key in ('详细描述', '