基于rsas html文档,导出IP高中危数据

对原代码进行了修改,添加了主机总数、高危漏洞总数、中危漏洞总数。代码如下:

"""
======================
@Auther:CacheYu
@Time:2019/11/18 下午
======================
"""
#!usr/bin/python
#coding:utf-8

import zipfile
from bs4 import BeautifulSoup
import sys
# import xlwings as xw  写入到excel中
import os 


#python3 输入方式:xxx.py 文件绝对地址
# 获取IP危险等级信息
def html(file):
	with zipfile.ZipFile(file,'r') as z:
		# print(z.namelist())
		f=z.open('index.html')
		souphtml = BeautifulSoup(f,'lxml')
		soup_hostsum = souphtml.find_all(class_="report_table plumb")
		hostsum = soup_hostsum[2].find(class_="even").find('td').get_text().replace(' ','').replace('\t','').replace('\n','').replace('\r','')
		print(hostsum)

		soupfind = souphtml.find_all(class_="report_content")
		table = soupfind[2].find(class_="report_table").find('tbody').find_all('tr')

		first_int = second_int = 0
		for trhtml in table:
			tdhtml = trhtml.find_all('td')
			ip = tdhtml[0].find('a').get_text()
			host = tdhtml[1].get_text()
			os = tdhtml[2].get_text()
			first = tdhtml[3].get_text()
			second = tdhtml[4].get_text()
			three = tdhtml[5].get_text()
			total = tdhtml[6].get_text()
			risk = tdhtml[7].get_text()
			iplist = ip + '\t' + host + '\t' + os + '\t' + first + '\t' + second + '\t' + three + '\t' + total + '\t' + risk + '\n'
			outputfile.write(iplist)
			
			first_int = first_int + int(first)
			second_int = second_int + int(second)
		print("firstsum:"+str(first_int)+";secondsum:"+str(second_int))
		f.close()


if __name__ =='__main__':
	outputfile=open("./html.txt",'w') #w表示复盖,a+表示追加
	outputfile.write('IP地址\t主机名\t操作系统\t高危\t中危\t低危\t总计\t主机风险值\n')
	file = sys.argv[1]
	html(file)
	outputfile.close()




----------------------------------------------------------------------------------------------------------------------------------------------------------------------

使用python3写了rsasIP的高中低列表,使用Beautifulsoup。将获取的列表写入txt中。(本来想使用xlwings一步到位写入excel中,但是写入其中涉及到sheet表的命名,将原本的string类型转成列表类型等目前没有想明白的问题,所以暂时写入txt中,简化了“解压压缩包,打开html文件”的步骤。

代码比较简单,如下所示:

"""
======================
@Auther:CacheYu
@Time:2019/11/11 下午
======================
"""
#!usr/bin/python
#coding:utf-8

import zipfile
from bs4 import BeautifulSoup
import sys
import os 


#python3 输入方式:xxx.py 文件绝对路径+文件名

# 获取IP危险等级信息
def html(file):
	with zipfile.ZipFile(file,'r') as z:
		# print(z.namelist())
		f=z.open('index.html')
		souphtml = BeautifulSoup(f,'lxml')
		soupfind = souphtml.find_all(class_="report_content")
		table = soupfind[2].find(class_="report_table").find('tbody').find_all('tr')
		for trhtml in table:
			tdhtml = trhtml.find_all('td')
			ip = tdhtml[0].find('a').string
			first = tdhtml[3].string
			second = tdhtml[4].string
			three = tdhtml[5].string
			iplist = ip + '\t' + first + '\t' + second + '\t' + three + '\n'
			outputfile.write(iplist)	
		f.close()


if __name__ =='__main__':
	outputfile=open("./html.txt",'w') #w表示复盖,a+表示追加
	outputfile.write('IP地址\t高危\t中危\t低危\n')
	file = sys.argv[1]
	html(file)
	outputfile.close()




运行结果:

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章