CQUT校园通知网消息爬虫+Server酱微信推送

上了大三之后发现很多学校的通知都不会发送到班群里面,导致自己会错过很多重要信息,故想写一个爬虫来获取从当前时间之后的新的通知标题,并推送到微信上。

PS:推送到微信上这个想法来源是,很多时候都需要将消息以一种载体传送给我们,以前经常用的是邮件,但是随着聊天工具的普及,微信在国人的生活中占据了越来越重要的地位,所以用来做消息提示正好;而为什么会想到Server酱,原因是在HW期间,为了和同事一起检测设备是否存活,使用Python编写了判断设备存活的脚本,并将实时将挂掉的设备使用Server酱实时推送到微信上,这样就不用定期去人工检查设备,大大解放了双手(计算机本就是用来解放双手的

整个过程很简单:

获取网页源码->解析源码并获取通知列表->与旧列表对比,将新的通知整理->通过Server酱接口推送到微信上

CQUT的通知网网址为:

https://tz.cqut.edu.cn/

获取源码,使用requests库即可

import requests
def GetRep(url):
    try:
        rep=requests.get(url,timeout=5)
        rep.encoding='utf-8'
        return rep.text
    except Exception as e:
        print(e)
        return "NULL"

尝试了一下,加不加请求头都可以,为了简便就不加

另外因为出现了requests获取网站相应text中文乱码的问题,所以使用语句

rep.encoding='utf-8'

将rep使用 utf-8 进行编码,其解决思路来源:https://blog.csdn.net/chaowanghn/article/details/54889835

进行目标源码的解析,很容易可以看到我们想要获取到的标题所在的位置:

所以使用xpath解析的代码部分为:

def GetNotice(rep):
    html = etree.HTML(rep)
    result = html.xpath('//span[@class="head"]/text()')
    return result

返回的result是当前捕获通知的列表,接着是与旧的列表进行比较,,同时将新的通知信息整理到message里面,在微信公众号推送消息中的换行不知道咋弄,所以直接使用 ### 三个#号来进行消息的分割,这一步也很简单:

def CheckNotice(oldlist,noticelist):
    message=""
    for notice in noticelist:
        if notice not in oldlist:
            message+=notice+"###"
    return message

最后是将信息通过Server酱的API接口发送到微信上,关于Server酱,你需要了解的是:

http://sc.ftqq.com/3.version

http://www.jeepxie.net/article/727449.html

其使用语法为:

# encoding:utf-8
import requests
api = "https://sc.ftqq.com/{YOUR-KEY}.send"
title = u"紧急通知"
content = """
#服务器又炸啦!
##请尽快修复服务器
"""
data = {
   "text":title,
   "desp":content
}
req = requests(api,data = data)

上面是在python2.x下的规则,当在python3.x的环境下时,需要将最后一句修改为:

req = requests.post(api, data=data)

在我们这个项目中这部分的代码为:

import requests
api = "https://sc.ftqq.com/{YOUR-KEY}.send"
def SendNotice(message):
    try:
        title="CQUT新通知"
        data = {
            "text": title,
            "desp": message
        }
        # print("4")
        req = requests.post(api, data=data)
        # print("5")
    except Exception as e:
        print(e)
        pass
    return

注意这一行

api = "https://sc.ftqq.com/{YOUR-KEY}.send"

这里的{YOUR-KEY}是需要你自己用GitHub账号登入网站后获得的,网站就是这个网站

http://sc.ftqq.com/3.version

其KEY所在位置在:

当然官方使用文档更详细,嘿嘿

 

最后就是整个项目的源代码,查资料加写代码花了一小段时间趴,主要是复习一下爬虫和Server酱的用法,{YOUR-KEY}需要自己替换,以下:

import requests
from lxml import etree
import time
​
api = "https://sc.ftqq.com/{YOUR-KEY}.send"
​
​
def GetRep(url):
    try:
        rep=requests.get(url,timeout=5)
        rep.encoding='utf-8'
        return rep.text
    except Exception as e:
        print(e)
        return "NULL"
​
def GetNotice(rep):
    html = etree.HTML(rep)
    result = html.xpath('//span[@class="head"]/text()')
    return result
​
def SendNotice(message):
    try:
        title="CQUT新通知"
        data = {
            "text": title,
            "desp": message
        }
        # print("4")
        req = requests.post(api, data=data)
        # print("5")
    except Exception as e:
        print(e)
        pass
    return
​
def CheckNotice(oldlist,noticelist):
    message=""
    for notice in noticelist:
        if notice not in oldlist:
            message+=notice+"###"
    return message
​
def main():
    url='https://tz.cqut.edu.cn/'
    rep = GetRep(url)
    oldlist = GetNotice(rep)
    while True:
        print("now start get_notice , time is: ")
        print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
        rep1 = GetRep(url)
        # print("1")
        noticelist = GetNotice(rep1)
        if oldlist!=noticelist:
            # print("2")
            message=CheckNotice(oldlist,noticelist)
            SendNotice(message)
            oldlist=noticelist
            # print("3")
        time.sleep(600)
​
if __name__=='__main__':
    main()

  

丢到服务器上跑,十分钟检测一次是否有新的通知,有的话将列表更新,并发送消息到微信,明天看看微信推送情况再进行贴图更新或者代码修改。

:D

 

二更

 

成功运行,可以进一步考虑将python程序长久运行在服务器后台

 

:D

 

原文地址:https://www.cnblogs.com/Cl0ud/p/13682365.html