PU口袋活动监控,为了让学生变得更加优秀<del>变成分奴</del>,学校要求学生在PU口袋参加活动从而获得第二课堂学分。便诞生了这个脚本......也是理论和实践相结合了吧。
直接复制,修改关键部分就可以使用了。
import requests
import os
import re
import time
from lxml import etree
import smtplib
import random
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
title = re.compile(".\*?<title>(?P<name>.\*?)</title>")
user\_agent\_list = ["Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; WOW64) Gecko/20100101 Firefox/61.0",
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86\_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
"Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10.5; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15",
]
header = {'User-Agent': random.choice(user\_agent\_list),"Cookie": "XXXXXXXXXX"}
def get\_page\_url():
url\_list=[]
for num in range(1,6):
url = f"https://XXX.pocketuni.net/index.php?app=event&mod=School&act=board&cat=all&sid=XXX&p={num}"
url\_list.append(url)
return url\_list
def handle(url\_list):
child\_url=[]
for url in url\_list:
resp = requests.get(url,headers=header)
resp.close()
resp\_tree=etree.HTML(resp.text)
child\_url.extend(resp\_tree.xpath("/html/body/div[2]/div/div[3]/div[2]/div[1]/ul/li/div[2]/div[1]/a/@href"))
return child\_url
def handle2(child\_urls):
x=1
active\_name\_list=[]
for url in child\_urls:
resp = requests.get(url,headers=header)
resp.close()
active\_name =str(title.findall(resp.text)).replace("[","").replace("]","").replace("'","").replace(" ","")
active\_name\_list.append(active\_name)
return active\_name\_list
def send\_email(text):
smtpsrever = 'smtp.163.com'
em = 'XXX@163.com'
yz = 'XXXXXX'
# 接收邮件的邮箱
receiver = 'XXXXXX@qq.com'
# 创建邮件对象
message = MIMEMultipart('relate') # 生成一个带附件的邮件对象
message = MIMEText(f'{text}', 'plain', 'utf-8')
subject = "爱心提醒" # 邮件的主题
# 把邮件的信息组装到邮件对象里
message['from'] = em
message['to'] = receiver
message['subject'] = subject
smtp = smtplib.SMTP()
smtp.connect(smtpsrever)
smtp.login(em, yz)
smtp.sendmail(em, receiver, message.as\_string())
smtp.quit()
if \_\_name\_\_=='\_\_main\_\_':
old\_list=[]
x=1
while True:
url\_list=get\_page\_url()
child\_urls=handle(url\_list)
active\_name\_list=handle2(child\_urls)
if x==1:
old\_list = old\_list+active\_name\_list
send\_email(old\_list)
x+=1
update\_list = [x for x in active\_name\_list if x not in old\_list]
f = open('D:\\pocketuni.log', 'a+', encoding='utf-8')
if len(update\_list)==0:
print("未更新,最近一次查看时间为:",time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),file=f)
else:
print("检测到更新,已推送。推送时间为:",time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),file=f)
old\_list = old\_list + update\_list
send\_email(update\_list)
f.close()
time.sleep(900)
只需要改一下cookie、邮箱、授权码,如果有需要还可以改下过滤条件(这里我只要有计算机学院的新活动就会推送)发送邮箱要开通POP3/SMTP服务获取授权码。
每15分钟查看一次活动列表,执行的结果我存到了外部文件中,可以到D:\pocketuni.log文件中查看执行结果,如果有活动更新会邮箱提醒。
可以将程序打包成.exe可执行文件:
pyinstaller -F -w -i p.ico pk.py
-F 打包一个单一的文件
-w 程序执行的时候不会启动命令行(只对windows有效)
-i 参数是指定打包后的图标,ico图标转换失败,那就先安装Pillow库
打包后最后一行会看到success就证明打包成功。然后在生成的dist目录下找到.exe可执行文件。
双击启动,在任务管理器中查看进程。也可以设置成开机自启。
执行的日志记录:
邮箱提醒:
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。