編輯中...php
#! /usr/bin/env python # -*- coding:utf-8 -*- # Author: Tdcqma ''' 網頁爬蟲,版本 2017-09-20 21:16 ''' import urllib.request import ssl,re import smtplib,email import datetime url="http://www.nsfocus.net/index.php?act=sec_bug&type_id=&os=&keyword=&page=1" request = urllib.request.Request(url) # 當嘗試訪問https開始當站點時,設置全局取消SSL證書驗證 ssl._create_default_https_context = ssl._create_unverified_context response = urllib.request.urlopen(request) data = response.read().decode('utf-8') # 保存當前日期到today變量裏 today = str(datetime.date.today()) # 使用正則表達式匹配指定安全警報內容 str = "<li><span>"+today+".*" res = re.findall(str,data) # 發送郵件 chst = email.charset.Charset(input_charset = 'utf-8') header = ("From: %s\nTo: %s\nSubject: %s\n\n" % ("from_mail@163.com", "to_mail@163.com", chst.header_encode("[爬蟲安全通告-綠盟]"))) # 打開本地文件,目的爲將抓取到的報警信息寫入文件 f = open("secInfo-lvmeng.txt",'w',encoding='utf-8') # 生產列表保存全部安全漏洞標題 tomcat_sec = ["Apache Tomcat 漏洞告警(當前生產版本爲7.0.68)\n\n"] Cisco_sec = ["Cisco 漏洞告警(當前生產版本爲1.0.35)\n\n"] WebLogic_sec = ["WebLogic 漏洞告警(當前生產版本爲10.33)\n\n"] tomcat_msg = ''' ''' WebLogic_msg = ''' ''' Cisco_msg = ''' ''' msg = ''' ''' for line in res: tomcat_title = "Apache Tomcat 漏洞告警(當前生產版本爲7.0.68)" sub_url = "http://www.nsfocus.net"+line[37:50] # 獲取安全漏洞連接 vul_title = line[52:-40] # 獲取安全漏洞標題 if "Apache Tomca" in vul_title: line = " ♠ "+today+" "+vul_title+"\n >> "+sub_url+'\n\n' tomcat_msg += line # 逐行讀取,將其保存到msg字符變量裏。 tomcat_sec.append(line) elif "WebLogic" in vul_title: line = " ♠ " + today + " " + vul_title + "\n >> " + sub_url + '\n\n' WebLogic_msg += line WebLogic_sec.append(line) elif "Cisco" in vul_title: line = " ♠ " + today + " " + vul_title + "\n >> " + sub_url + '\n\n' Cisco_msg += line Cisco_sec.append(line) msg = [tomcat_sec,WebLogic_sec,Cisco_sec] secu_msg = ''' ''' for i in range(len(msg)): if len(msg[i]) > 1: for j in range(len(msg[i])): secu_msg += msg[i][j] f.writelines(secu_msg) f.close() msg = secu_msg # 借用163smtp服務器發送郵件,將上面讀到的報警信息做爲郵件正文發送。 email_con = header.encode('utf-8') + msg.encode('utf-8') smtp = smtplib.SMTP("smtp.163.com") smtp.login("from_mail@163.com","from_mail_pass") smtp.sendmail('from_mail@163.com','to_mail@163.com',email_con) print('mail send success!') smtp.quit()
獲取爬蟲郵件截圖:python