feat: 项目初始化
This commit is contained in:
parent
008aa3247c
commit
120df8ea49
|
|
@ -0,0 +1,74 @@
|
|||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
import smtplib
|
||||
from email.mime.text import MIMEText
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
import time
|
||||
import logging
|
||||
|
||||
# 配置日志
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(message)s")
|
||||
|
||||
# 配置邮件发送函数
|
||||
def send_email(subject, body, to_email):
|
||||
# 邮件服务器配置
|
||||
smtp_server = "smtp.lcdd.net"
|
||||
smtp_port = 587
|
||||
from_email = "admin@lcdd.net"
|
||||
password = "lichao1314"
|
||||
|
||||
# 构建邮件
|
||||
msg = MIMEMultipart()
|
||||
msg["From"] = from_email
|
||||
msg["To"] = to_email
|
||||
msg["Subject"] = subject
|
||||
msg.attach(MIMEText(body, "plain"))
|
||||
|
||||
# 连接到服务器并发送邮件
|
||||
try:
|
||||
with smtplib.SMTP(smtp_server, smtp_port) as server:
|
||||
server.starttls()
|
||||
server.login(from_email, password)
|
||||
server.sendmail(from_email, to_email, msg.as_string())
|
||||
logging.info("邮件已发送到 %s", to_email)
|
||||
except Exception as e:
|
||||
logging.error("邮件发送失败: %s", e)
|
||||
|
||||
# 配置爬虫任务
|
||||
def check_website_for_condition(url, keyword):
|
||||
try:
|
||||
response = requests.get(url)
|
||||
response.raise_for_status() # 确保请求成功
|
||||
soup = BeautifulSoup(response.text, "html.parser")
|
||||
|
||||
# 搜索关键词
|
||||
if keyword in soup.get_text():
|
||||
logging.info("检测到关键词: %s", keyword)
|
||||
return True
|
||||
else:
|
||||
logging.info("关键词 %s 不存在", keyword)
|
||||
return False
|
||||
except Exception as e:
|
||||
logging.error("请求失败: %s", e)
|
||||
return False
|
||||
|
||||
# 主调度函数
|
||||
def monitor_website():
|
||||
url = "https://lcdd.net" # 目标网址
|
||||
keyword = "React" # 目标关键词
|
||||
to_email = "randolphreidhdu@gmail.com" # 收件人邮箱
|
||||
|
||||
# 监控循环
|
||||
while True:
|
||||
if check_website_for_condition(url, keyword):
|
||||
subject = "关键词检测通知"
|
||||
body = f"目标网站 {url} 上检测到关键词 '{keyword}' 出现!"
|
||||
send_email(subject, body, to_email)
|
||||
break # 如果检测到条件,发送一次邮件并退出循环
|
||||
|
||||
# 未检测到条件时,延迟一段时间再次检查
|
||||
time.sleep(300) # 每5分钟检查一次
|
||||
|
||||
# 启动监控
|
||||
if __name__ == "__main__":
|
||||
monitor_website()
|
||||
Loading…
Reference in New Issue