基於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()




運行結果:

 

 

 

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