前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自动监控网站消息动态并邮件通知 [附代码]

自动监控网站消息动态并邮件通知 [附代码]

作者头像
小锋学长生活大爆炸
发布2022-05-09 15:57:14
5370
发布2022-05-09 15:57:14
举报

目录

背景

思路

实现

背景

我们可能有时需要关注某个网站的通知更新,以便进行后续操作。比如,时常查看官网的报名通知。但如果手动去看,比较麻烦也容易忘记,所以如果有程序自动监控就比较方便。

思路

定时使用Python爬取网站消息列表,对比是否有新消息发出,如果有的话,就邮件通知自己。

实现

比如这个报名网站,先看他的结构。

我们准备使用xpath来抓取他:

为了方便存储和查询,我们把title转为hash值,用到的python自带的hash()函数

综上,对应的代码就是:

代码语言:javascript
复制
import requests
from lxml import etree

def get_notification():
    store_map = {}
    url = r'https://yz.scut.edu.cn/bszs/list.htm'
    try:
        html = requests.get(url).content.decode('utf-8')
    except:
        return None
    node = etree.HTML(html)
    for i in node.xpath('/html/body/section/div[2]/div/div[2]/div[3]/div/div/div/div[1]/ul//li/div'):
        title = i.xpath('a[2]/text()')[0]
        href = i.xpath('a[2]/@href')[0]
        href = href if href.startswith('https') else 'https://yz.scut.edu.cn/' + href
        date = i.xpath('span[1]/text()')[0]
        title_hash = hash(title)
        store_map[title_hash] = {'title': title, 'href': href, 'date': date}
    return store_map

然后要对比前后两次抓取的内容是否一样,这里使用numpy的差集setdiff1d()函数来实现。

代码示例如下:

代码语言:javascript
复制
saved_store = get_notification()
saved_lists = np.array(list(saved_store.keys()))

new_store = get_notification()
new_lists = np.array(list(new_store.keys()))

diff = np.setdiff1d(saved_lists, new_lists)

如果差集结果不为空,那就是有新通知了,那就要发邮件提醒。

综上,对应的代码就是:

代码语言:javascript
复制
import time
import numpy as np

def start():
    saved_store = get_notification()
    if saved_store is None:
        print('启动失败!')
        return
    saved_lists = np.array(list(saved_store.keys()))

    while True:
        print('监听中...')
        new_store = get_notification()
        if new_store is None:
            print('获取失败,将重试!')
            time.sleep(5)
            continue
        new_lists = np.array(list(new_store.keys()))
        diff = np.setdiff1d(saved_lists, new_lists)
        if diff.size != 0:
            print('有新通知!')
            new_noti = []
            for i in diff:
                new_noti.append(new_store[i])
            print(new_noti)
            youxiang('研招有新通知发出', str(new_noti), '1061700625@qq.com')
            saved_store = new_store
            saved_lists = np.array(list(saved_store.keys()))
        time.sleep(60*10)

最后,还有个发邮箱的代码需要补充:

代码语言:javascript
复制
import smtplib
from email.mime.text import MIMEText

def youxiang(title, msg, receive):
    msg_from = 'xxxxxxx@qq.com'  # 发送方邮箱
    passwd = 'xxxxxx'  # 填入发送方邮箱的授权码
    receiver = [receive]  # 收件人邮箱
    subject = title  # 主题
    content = msg  # 正文
    msg = MIMEText(content)
    msg['Subject'] = subject
    msg['From'] = msg_from
    msg['To'] = ";".join(receiver)  # 收件人为多个收件人,通过join将列表转换为以;为间隔的字符串
    s = smtplib.SMTP_SSL("smtp.qq.com", 465)  # 邮件服务器及端口号
    try:
        s.login(msg_from, passwd)
        s.sendmail(msg_from, receiver, msg.as_string())
        print("邮件已发送成功!")
    except Exception as e:
        print(e)
    finally:
        s.quit()

最后,放到服务器上或者一些免费的云函数平台上跑就行了。

以上实现较为粗糙,仅供交流~

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-03-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景
  • 思路
  • 实现
相关产品与服务
云函数
云函数(Serverless Cloud Function,SCF)是腾讯云为企业和开发者们提供的无服务器执行环境,帮助您在无需购买和管理服务器的情况下运行代码。您只需使用平台支持的语言编写核心代码并设置代码运行的条件,即可在腾讯云基础设施上弹性、安全地运行代码。云函数是实时文件处理和数据处理等场景下理想的计算平台。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档