侧边栏壁纸
  • 累计撰写 4 篇文章
  • 累计创建 2 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

一个定时刷新网站等公告的python程序

musan
2025-06-03 / 0 评论 / 0 点赞 / 3 阅读 / 0 字
import requests
from bs4 import BeautifulSoup
import time
import threading
from plyer import notification  # 桌面通知

# 配置参数
URL = "https://www.ceshi.com"  # 目标网站
CHECK_INTERVAL = 1800  # 检查间隔(秒)
TARGET_TEXT = "2025-06-03"  # 要检测的内容


def check_website():
    try:
        # 发送HTTP请求(添加浏览器头部避免被屏蔽)
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
        }
        response = requests.get(URL, headers=headers, timeout=10)
        response.raise_for_status()  # 检查HTTP错误

        # 解析HTML内容
        soup = BeautifulSoup(response.text, 'html.parser')

        # 检测目标内容是否存在
        if TARGET_TEXT in soup.get_text():
            trigger_alert()
            return True
        return False

    except Exception as e:
        print(f"检测失败: {str(e)}")
        return False


def trigger_alert():
    """触发提醒"""
    print(f"✅ 检测到目标内容: {TARGET_TEXT}")

    # 1. 桌面通知
    try:
        notification.notify(
            title="网站内容更新",
            message=f"检测到: {TARGET_TEXT}",
            app_name="网站监控",
            timeout=10
        )
    except Exception as e:
        print(f"通知发送失败: {e}")



def monitor():
    """定时监控循环"""
    while True:
        print(f"{time.strftime('%Y-%m-%d %H:%M:%S')} 正在检查...")
        if check_website():
            print("提醒已触发! 等待10分钟后继续...")
            time.sleep(600)  # 找到内容后暂停10分钟
        else:
            time.sleep(CHECK_INTERVAL)


if __name__ == "__main__":
    print(f"🕵️ 开始监控网站: {URL}")
    print(f"🔍 查找内容: '{TARGET_TEXT}'")
    print(f"⏱ 检查间隔: {CHECK_INTERVAL}秒")
    monitor()

0

评论区