Python实现网站违禁词检测

发布时间:2025-02-05 19:34:16 作者:同领天下 来源:本站 浏览量(85) 点赞(41)
摘要:import requestsfrom bs4 import BeautifulSoupimport timeimport smtplibimport playsoundfrom email.mime.text import MIMETextimport yamlimport loggingfrom concurrent.futures import ThreadPoolExecutorclass WebsiteMonitor: def __init__(self): self.load_config() 
import requests
from bs4 import BeautifulSoup
import time
import smtplib
import playsound
from email.mime.text import MIMEText
import yaml
import logging
from concurrent.futures import ThreadPoolExecutor

class WebsiteMonitor:
    def __init__(self):
        self.load_config()
        self.setup_logging()
        
    def load_config(self):
        with open('config.yml', 'r', encoding='utf-8') as f:
            self.config = yaml.safe_load(f)
        self.banned_words = self.config['banned_words']
        self.websites = self.config['websites']
        self.email_config = self.config['email']
        
    def setup_logging(self):
        logging.basicConfig(
            level=logging.INFO,
            format='%(asctime)s - %(levelname)s - %(message)s',
            filename='monitor.log'
        )
        
    def check_website(self, url):
        try:
            response = requests.get(url, timeout=10)
            soup = BeautifulSoup(response.text, 'html.parser')
            text_content = soup.get_text().lower()
            
            found_words = []
            for word in self.banned_words:
                if word.lower() in text_content:
                    found_words.append(word)
                    
            if found_words:
                self.alert(url, found_words)
                
        except Exception as e:
            logging.error(f"检查网站 {url} 时发生错误: {str(e)}")
            
    def alert(self, url, found_words):
        message = f"警告!在网站 {url} 中发现违禁词: {', '.join(found_words)}"
        logging.warning(message)
        
        # 发送邮件提醒
        self.send_email(message)
        
        # 播放声音提醒
        self.play_alert_sound()
        
    def send_email(self, message):
        try:
            msg = MIMEText(message)
            msg['Subject'] = '违禁词监测警告'
            msg['From'] = self.email_config['sender']
            msg['To'] = self.email_config['receiver']
            
            with smtplib.SMTP_SSL(self.email_config['smtp_server'], self.email_config['smtp_port']) as server:
                server.login(self.email_config['sender'], self.email_config['password'])
                server.send_message(msg)
                
        except Exception as e:
            logging.error(f"发送邮件时发生错误: {str(e)}")
            
    def play_alert_sound(self):
        try:
            playsound.playsound('alert.mp3')
        except Exception as e:
            logging.error(f"播放提醒声音时发生错误: {str(e)}")
            
    def start_monitoring(self):
        logging.info("开始监测网站...")
        while True:
            with ThreadPoolExecutor(max_workers=5) as executor:
                executor.map(self.check_website, self.websites)
            time.sleep(self.config['check_interval'])

if __name__ == '__main__':
    monitor = WebsiteMonitor()
    monitor.start_monitoring()


二维码

扫一扫,关注我们

声明:本文由【同领天下】编辑上传发布,转载此文章须经作者同意,并请附上出处【同领天下】及本页链接。如内容、图片有任何版权问题,请联系我们进行处理。

感兴趣吗?

欢迎联系我们,我们愿意为您解答任何有关网站疑难问题!

您身边的【网站建设专家】

搜索千万次不如咨询1次

主营项目:网站建设,手机网站,响应式网站,SEO优化,小程序开发,版权登记,商标注册等

立即咨询 18640246253
在线客服
嘿,我来帮您!