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()
版权归属:
musan
本文链接:
http://blog.imusan.net/archives/yi-ge-ding-shi-shua-xin-deng-zheng-fu-gong-gao-de-pythoncheng-xu
许可协议:
本文使用《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》协议授权
评论区