怎么用脚本检测网页是否更新

wen 实用脚本 2

本文目录导读:

怎么用脚本检测网页是否更新

  1. Python脚本(最常用)
  2. Bash脚本(Linux环境)
  3. JavaScript脚本(浏览器环境)
  4. 完整监控脚本(Python版)
  5. 使用建议

Python脚本(最常用)

基础版 - 检查网页内容变化

import requests
import hashlib
import time
def check_web_update(url, previous_hash=None):
    try:
        response = requests.get(url, timeout=10)
        response.encoding = 'utf-8'
        # 计算当前页面内容的哈希值
        current_hash = hashlib.md5(response.text.encode()).hexdigest()
        if previous_hash is None:
            return current_hash, "首次检查"
        elif current_hash != previous_hash:
            return current_hash, "页面已更新"
        else:
            return current_hash, "页面无变化"
    except Exception as e:
        return None, f"检查失败: {str(e)}"
# 使用示例
url = "https://example.com"
hash_value = None
while True:
    hash_value, status = check_web_update(url, hash_value)
    print(f"{time.strftime('%Y-%m-%d %H:%M:%S')} - {status}")
    time.sleep(300)  # 每5分钟检查一次

高级版 - 只检查特定内容

import requests
import re
from bs4 import BeautifulSoup
def check_specific_content(url, keyword, previous_content=None):
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')
    # 提取特定元素内容
    target_element = soup.find('div', class_='content')
    current_content = target_element.text.strip() if target_element else ""
    # 查找特定关键词
    if keyword in current_content:
        print(f"找到关键词: {keyword}")
    if current_content != previous_content:
        return current_content, True
    return current_content, False

Bash脚本(Linux环境)

#!/bin/bash
URL="https://example.com"
HASH_FILE="/tmp/page_hash.txt"
# 获取当前页面内容并计算哈希
current_hash=$(curl -s "$URL" | md5sum | cut -d' ' -f1)
# 检查历史哈希文件
if [ ! -f "$HASH_FILE" ]; then
    echo "$current_hash" > "$HASH_FILE"
    echo "首次检查,已记录页面哈希"
    exit 0
fi
# 比较哈希值
previous_hash=$(cat "$HASH_FILE")
if [ "$current_hash" != "$previous_hash" ]; then
    echo "$(date): 页面已更新!"
    echo "$current_hash" > "$HASH_FILE"
else
    echo "$(date): 页面无变化"
fi

JavaScript脚本(浏览器环境)

// 在浏览器控制台运行
async function checkPageUpdate() {
    const url = window.location.href;
    const storageKey = 'page_hash_' + url;
    // 获取当前页面内容
    const response = await fetch(url);
    const content = await response.text();
    // 计算简单哈希
    const currentHash = simpleHash(content);
    const previousHash = localStorage.getItem(storageKey);
    if (!previousHash) {
        localStorage.setItem(storageKey, currentHash);
        console.log('首次检查,已保存当前页面哈希');
        return false;
    }
    if (currentHash !== previousHash) {
        console.log('页面已更新!');
        localStorage.setItem(storageKey, currentHash);
        // 可以触发通知
        new Notification('页面更新提醒', {
            body: '目标页面内容已发生变化'
        });
        return true;
    }
    console.log('页面无变化');
    return false;
}
function simpleHash(str) {
    let hash = 0;
    for (let i = 0; i < str.length; i++) {
        const char = str.charCodeAt(i);
        hash = ((hash << 5) - hash) + char;
        hash |= 0;
    }
    return hash.toString();
}
// 每隔5分钟检查一次
setInterval(checkPageUpdate, 300000);

完整监控脚本(Python版)

import requests
import hashlib
import time
import smtplib
from email.mime.text import MIMEText
from datetime import datetime
class PageMonitor:
    def __init__(self, url, check_interval=300):
        self.url = url
        self.interval = check_interval
        self.last_hash = None
        self.last_content = None
    def get_page_hash(self):
        """获取页面哈希值"""
        try:
            response = requests.get(self.url, timeout=10)
            response.encoding = 'utf-8'
            content = response.text
            hash_value = hashlib.sha256(content.encode()).hexdigest()
            return hash_value, content
        except Exception as e:
            print(f"获取页面失败: {e}")
            return None, None
    def send_notification(self, message):
        """发送通知(示例为控制台输出)"""
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        print(f"[{timestamp}] {message}")
        # 可以添加邮件通知
        # self.send_email(message)
    def send_email(self, message):
        """发送邮件通知"""
        # 配置邮件参数
        sender = 'your_email@example.com'
        receiver = 'receiver@example.com'
        password = 'your_password'
        msg = MIMEText(f"页面 {self.url} 已更新\n\n{message}")
        msg['Subject'] = '页面更新通知'
        msg['From'] = sender
        msg['To'] = receiver
        try:
            server = smtplib.SMTP('smtp.gmail.com', 587)
            server.starttls()
            server.login(sender, password)
            server.send_message(msg)
            server.quit()
        except Exception as e:
            print(f"发送邮件失败: {e}")
    def start_monitoring(self):
        """开始监控"""
        print(f"开始监控页面: {self.url}")
        print(f"检查间隔: {self.interval}秒")
        while True:
            current_hash, current_content = self.get_page_hash()
            if current_hash is None:
                time.sleep(self.interval)
                continue
            if self.last_hash is None:
                self.last_hash = current_hash
                self.last_content = current_content
                self.send_notification("首次监控,已记录初始状态")
            elif current_hash != self.last_hash:
                self.send_notification("页面已更新!")
                self.last_hash = current_hash
                self.last_content = current_content
            else:
                print(f"[{datetime.now().strftime('%H:%M:%S')}] 页面无变化")
            time.sleep(self.interval)
# 使用示例
if __name__ == "__main__":
    monitor = PageMonitor(
        url="https://example.com",
        check_interval=300  # 5分钟
    )
    monitor.start_monitoring()

使用建议

  1. 频率控制:不要检查太频繁,避免给服务器造成负担
  2. 异常处理:添加超时和重试机制
  3. 存储方式:将哈希值持久化存储(文件、数据库)
  4. 通知方式:可以集成邮件、微信、钉钉等通知
  5. 增量更新:只比较特定区域而非整个页面

选择哪种方法取决于你的具体需求和技术环境。

抱歉,评论功能暂时关闭!