如何检测在线监控网站中div内的数值变化并触发通知?
实现在线监控网站数值变化检测方案
方案一:Python 定时检测(推荐新手)
Python上手简单,适合定时爬取网页并检测数值变化,无需复杂环境配置。
步骤1:安装依赖库
在终端执行:pip install requests beautifulsoup4步骤2:编写检测脚本
脚本会定时请求目标网页,提取数值并对比变化:import requests from bs4 import BeautifulSoup import time import os # 替换为你的监控网站地址 TARGET_URL = "https://your-monitor-site.com" # 用于记录上次数值的文件路径 LAST_VALUE_FILE = "last_disaster_value.txt" def get_current_value(): try: # 请求网页 response = requests.get(TARGET_URL) response.raise_for_status() # 解析HTML soup = BeautifulSoup(response.text, 'html.parser') count_span = soup.find('span', class_='count') if count_span: return int(count_span.text.strip()) return None except Exception as e: print(f"获取数值失败: {str(e)}") return None def main(): # 初始化上次数值 last_value = 0 if os.path.exists(LAST_VALUE_FILE): with open(LAST_VALUE_FILE, 'r') as f: last_value = int(f.read().strip()) # 循环检测 while True: current_value = get_current_value() if current_value is not None: print(f"当前数值: {current_value}") # 检测异常:从0变为1及以上 if last_value == 0 and current_value >= 1: print("触发异常通知!") # 这里调用你已实现的通知逻辑(如发邮件) # send_notification(current_value) # 更新上次数值 last_value = current_value with open(LAST_VALUE_FILE, 'w') as f: f.write(str(last_value)) # 每隔5分钟检测一次(单位:秒,可自行调整) time.sleep(300) if __name__ == "__main__": main()步骤3:运行脚本
直接执行python monitor.py,可借助工具让脚本后台持续运行:- Linux:使用
nohup python monitor.py & - Windows:创建任务计划,定时启动脚本
- Linux:使用
方案二:浏览器油猴脚本(无需服务器)
如果不想搭建服务器,可通过浏览器插件实现网页端实时检测。
步骤1:安装Tampermonkey插件
在浏览器应用商店搜索并安装「Tampermonkey」(油猴插件)。步骤2:新建用户脚本
打开Tampermonkey控制台,新建脚本,粘贴以下代码:// ==UserScript== // @name 灾难数值监控脚本 // @namespace http://tampermonkey.net/ // @version 0.1 // @description 监控指定span数值变化,异常时触发通知 // @author You // @match https://your-monitor-site.com/* // @grant none // ==/UserScript== (function() { 'use strict'; let lastValue = 0; // 检测间隔(毫秒,这里设为30秒) const CHECK_INTERVAL = 30000; function checkValue() { const countSpan = document.querySelector('span.count'); if (!countSpan) return; const currentValue = parseInt(countSpan.textContent.trim()); if (isNaN(currentValue)) return; console.log(`当前数值: ${currentValue}`); // 检测异常触发条件 if (lastValue === 0 && currentValue >= 1) { // 请求浏览器通知权限并发送通知 if (Notification.permission === "granted") { new Notification("监控警报", { body: `当前数值为${currentValue},请及时处理!` }); } else if (Notification.permission !== "denied") { Notification.requestPermission().then(perm => { if (perm === "granted") { new Notification("监控警报", { body: `当前数值为${currentValue},请及时处理!` }); } }); } // 也可调用你已有的通知接口(如发邮件API) // fetch('https://your-notify-api.com', { // method: 'POST', // body: JSON.stringify({value: currentValue}) // }); } lastValue = currentValue; } // 首次执行检测 checkValue(); // 定时循环检测 setInterval(checkValue, CHECK_INTERVAL); })();步骤3:启用脚本
保存脚本后,打开你的监控网站,脚本会自动运行,记得允许浏览器的通知权限。
方案三:PHP + 服务器定时任务(适合已有服务器场景)
如果已有PHP环境,可编写PHP脚本配合服务器定时任务实现检测。
示例PHP代码:
<?php // 替换为你的监控网站地址 $targetUrl = "https://your-monitor-site.com"; // 记录上次数值的文件 $lastValueFile = "last_value.txt"; // 获取当前监控数值 function getCurrentValue($url) { $html = file_get_contents($url); if (!$html) return null; // 正则匹配span.count中的数值 preg_match('/<span class="count">(\d+)<\/span>/', $html, $matches); return isset($matches[1]) ? intval($matches[1]) : null; } $currentValue = getCurrentValue($targetUrl); if ($currentValue === null) { exit("无法获取监控数值\n"); } // 读取上次记录的数值 $lastValue = 0; if (file_exists($lastValueFile)) { $lastValue = intval(file_get_contents($lastValueFile)); } // 检测异常并触发通知 if ($lastValue === 0 && $currentValue >= 1) { echo "数值异常,当前值:{$currentValue}\n"; // 调用你已实现的通知逻辑(如发邮件) // mail('your-email@example.com', '监控警报', '当前数值为' . $currentValue); } // 更新上次数值记录 file_put_contents($lastValueFile, strval($currentValue)); ?>设置定时任务
在Linux服务器中,通过crontab设置每5分钟运行一次脚本:*/5 * * * * php /path/to/your/monitor.php
内容的提问来源于stack exchange,提问作者marche14
相关产品推荐
相关产品推荐

