【网站可用性自动化监测】python+seleium
前倾提要:做了网络安全后重保期间需要对用户系统每日进行监测,相关监测脚本网上其实也有很多,但能自己个性化做一下也挺好,太久没做小程序需要写下代码巩固。
本次程序开发目的是重保期间每天分三个时间点对多个网站进行访问监测可用性,并截图,如发现异常把异常站点推送至微信助手并记录
分三大功能函数:网站访问、网站列表读取、定时执行函数
第一部分网站列表读取 代码片 .
def get_page_png(urls, browser,now_folder):
die_domain = []
'''
从一个地址列表里,逐个访问,可以访问的地址就截图
'''
for url in urls:
png_namea = url.replace('//', '_').replace('.', '_').replace('/', '_').replace(':',"_")
png_name = png_namea + '.png'
try:
urllib3.disable_warnings()
response = requests.get(url=url, headers=headers, timeout=10, verify=False)
if response.status_code == 200:
browser.get(url)
browser.save_screenshot(f"./{now_folder}/{png_name}")
print(f"{png_name} 保存成功")
except:
now_404 = f"{url} 无法访问!"
print(now_404)
mychat(now_404)
die_domain.append(url)
pass
now_die_domain(die_domain)
第二部分网站访问 代码片 .
def main():
'''
主函数
'''
print(time.strftime("%Y-%m-%d %H:%M"))
urls = []
with open("domain_list.txt", "r") as f:
for domain in f.readlines():
urls.append(domain.strip())
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_argument('--headless')
options.add_argument('--disable_gpu')
browser = webdriver.Chrome(options=options)
browser.maximize_window()
now_folder = new_folder()
get_page_png(urls, browser,now_folder)
browser.quit()
第三部分定时执行 代码片 .
def mytime():
schedule.every().day.at("10:00").do(main)
schedule.every().day.at("13:00").do(main)
schedule.every().day.at("16:00").do(main)
while True:
try:
schedule.run_pending()
time.sleep(1)
except Exception as e:
print('报错:',e)
完整代码 代码片 .
import requests
import os
import time
import urllib3
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import schedule
def new_folder():
s = time.strftime("%Y%m%d%H%M")
os.mkdir("http_pic" + s)
f = "http_pic" + s
return f
def get_page_png(urls, browser,now_folder):
die_domain = []
'''
从一个地址列表里,逐个访问,可以访问的地址就截图
'''
for url in urls:
png_namea = url.replace('//', '_').replace('.', '_').replace('/', '_').replace(':',"_")
png_name = png_namea + '.png'
try:
urllib3.disable_warnings()
response = requests.get(url=url, headers=headers, timeout=10, verify=False)
if response.status_code == 200:
browser.get(url)
browser.save_screenshot(f"./{now_folder}/{png_name}")
print(f"{png_name} 保存成功")
except:
now_404 = f"{url} 无法访问!"
print(now_404)
die_domain.append(url)
pass
now_die_domain(die_domain)
def now_die_domain(die_domain):
if die_domain:
f = open("无法访问的站点.txt", "a")
f.write(time.strftime(f"%Y-%m-%d %H:%M") + "\n")
for domain in die_domain:
f.write(domain + "\n")
f.close()
def main():
'''
主函数
'''
print(time.strftime("%Y-%m-%d %H:%M"))
urls = []
with open("domain_list.txt", "r") as f:
for domain in f.readlines():
urls.append(domain.strip())
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_argument('--headless')
options.add_argument('--disable_gpu')
browser = webdriver.Chrome(options=options)
browser.maximize_window()
now_folder = new_folder()
get_page_png(urls, browser,now_folder)
browser.quit()
def mychat(now_404):
a = now_404
itchat.send(a, toUserName='filehelper')
def mytime():
schedule.every().day.at("10:00").do(main)
schedule.every().day.at("13:00").do(main)
schedule.every().day.at("16:00").do(main)
while True:
try:
schedule.run_pending()
time.sleep(1)
except Exception as e:
print('报错:',e)
if __name__ == '__main__':
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36",
}
mytime()
程序运行效果 代码片
.
备注:seleium需要指定浏览器和匹配driver文件,本脚本使用的是chrome及对应版本driver(driver下载链接http://chromedriver.storage.googleapis.com/index.html)
|