CQUT校园通知网消息爬虫+Server酱微信推送
上了大三之后發(fā)現(xiàn)很多學(xué)校的通知都不會(huì)發(fā)送到班群里面,導(dǎo)致自己會(huì)錯(cuò)過很多重要信息,故想寫一個(gè)爬蟲來獲取從當(dāng)前時(shí)間之后的新的通知標(biāo)題,并推送到微信上。
PS:推送到微信上這個(gè)想法來源是,很多時(shí)候都需要將消息以一種載體傳送給我們,以前經(jīng)常用的是郵件,但是隨著聊天工具的普及,微信在國人的生活中占據(jù)了越來越重要的地位,所以用來做消息提示正好;而為什么會(huì)想到Server醬,原因是在HW期間,為了和同事一起檢測(cè)設(shè)備是否存活,使用Python編寫了判斷設(shè)備存活的腳本,并將實(shí)時(shí)將掛掉的設(shè)備使用Server醬實(shí)時(shí)推送到微信上,這樣就不用定期去人工檢查設(shè)備,大大解放了雙手(計(jì)算機(jī)本就是用來解放雙手的
整個(gè)過程很簡(jiǎn)單:
獲取網(wǎng)頁源碼->解析源碼并獲取通知列表->與舊列表對(duì)比,將新的通知整理->通過Server醬接口推送到微信上
CQUT的通知網(wǎng)網(wǎng)址為:
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"
嘗試了一下,加不加請(qǐng)求頭都可以,為了簡(jiǎn)便就不加
另外因?yàn)槌霈F(xiàn)了requests獲取網(wǎng)站相應(yīng)text中文亂碼的問題,所以使用語句
rep.encoding='utf-8'
將rep使用 utf-8 進(jìn)行編碼,其解決思路來源:https://blog.csdn.net/chaowanghn/article/details/54889835
進(jìn)行目標(biāo)源碼的解析,很容易可以看到我們想要獲取到的標(biāo)題所在的位置:
所以使用xpath解析的代碼部分為:
def GetNotice(rep):
html = etree.HTML(rep)
result = html.xpath('//span[@class="head"]/text()')
return result
返回的result是當(dāng)前捕獲通知的列表,接著是與舊的列表進(jìn)行比較,,同時(shí)將新的通知信息整理到message里面,在微信公眾號(hào)推送消息中的換行不知道咋弄,所以直接使用 ### 三個(gè)#號(hào)來進(jìn)行消息的分割,這一步也很簡(jiǎn)單:
def CheckNotice(oldlist,noticelist):
message=""
for notice in noticelist:
if notice not in oldlist:
message+=notice+"###"
return message
最后是將信息通過Server醬的API接口發(fā)送到微信上,關(guān)于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 = """
#服務(wù)器又炸啦!
##請(qǐng)盡快修復(fù)服務(wù)器
"""
data = {
"text":title,
"desp":content
}
req = requests(api,data = data)
上面是在python2.x下的規(guī)則,當(dāng)在python3.x的環(huán)境下時(shí),需要將最后一句修改為:
req = requests.post(api, data=data)
在我們這個(gè)項(xiàng)目中這部分的代碼為:
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賬號(hào)登入網(wǎng)站后獲得的,網(wǎng)站就是這個(gè)網(wǎng)站
http://sc.ftqq.com/3.version
其KEY所在位置在:
當(dāng)然官方使用文檔更詳細(xì),嘿嘿
最后就是整個(gè)項(xiàng)目的源代碼,查資料加寫代碼花了一小段時(shí)間趴,主要是復(fù)習(xí)一下爬蟲和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()
丟到服務(wù)器上跑,十分鐘檢測(cè)一次是否有新的通知,有的話將列表更新,并發(fā)送消息到微信,明天看看微信推送情況再進(jìn)行貼圖更新或者代碼修改。
:D
二更
成功運(yùn)行,可以進(jìn)一步考慮將python程序長久運(yùn)行在服務(wù)器后臺(tái)
:D
總結(jié)
以上是生活随笔為你收集整理的CQUT校园通知网消息爬虫+Server酱微信推送的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 20165223《Java程序设计》第八
- 下一篇: 捕获程序异常之onerror