咱们图书馆其实书更新的挺快,各方面都还能够的,可是没有过时提醒的功能,致使我屡次超期罚现,很不爽,是时候来个脚本搞定他了python
以前关注了学习的官方微信公众号,介个能够查询信息,好就从这里入手,在浏览器中打开开始分析网页的运行状况,来提取出APIchrome
chrome浏览器F12
->Network
,其实分析这些API真的须要耐心,多试试。忘了截图了T_Tapi
看代码吧:浏览器
#!/usr/bin/env python #coding: utf-8 import urllib2 import urllib import cookielib from ast import literal_eval from time import localtime import datetime import smtplib from email.mime.text import MIMEText url = "https://api.xiyoumobile.com/xiyoulibv2/user/login" #登陆API, 求求不要攻击咱们学校API!!! rent_url = "https://api.xiyoumobile.com/xiyoulibv2/user/rent" #借书信息查询API user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)" headers = {"User-Agent": user_agent, "Referer": "https://lib.xiyoumobile.com/" } #建立头部,以防有防盗链给拒了 mailto_list = ["xxxxxx@qq.com"] #通知邮箱 mail_host = "smtp.163.com" mail_user = "xxxxxxxx@163.com" #发件邮箱 mail_pass = "xxxxxxx" #发件邮箱密码 def main(): try: cookies = cookielib.CookieJar() request_data = urllib.urlencode({ #提交的数据, 方式竟然是GET,呵呵 'username': 'student_ID', 'password': 'password' }) opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies)) #建立opener request = urllib2.Request(url=url, data=request_data, headers=headers) response = opener.open(request) rst = response.read().decode('utf-8') rst = literal_eval(rst.replace('true','True').replace('false','False')) #获得sessionID和path路径,玩的小把戏吧 status, detail = rst["Result"], rst["Detail"] #返回来是status,和上面说的sessionID和path if status: data = urllib.urlencode({"session": detail}) #拿到sessionID就能够直接请求了 request = urllib2.Request(rent_url, data) response = urllib2.urlopen(request) rent_info = response.read().decode('utf-8') #请求以后就会返回借书的信息,作些处理就OK了 rent_info = literal_eval(rent_info.replace('true','True').replace('false','False')) status_rent, rent_detail = rent_info["Result"], rent_info["Detail"] if status_rent: send_info = '' now_time = localtime() now_date = datetime.date(now_time.tm_year, now_time.tm_mon, now_time.tm_mday) for book in rent_detail: d = book['Date'] deadline = datetime.date(int(d[:4]), int(d[4:6]), int(d[6:])) delta = (deadline - now_date).days if delta <= 2: send_info += book['Title'] if book['CanRenew']: send_info += ' 能够续借 ' else: send_info += ' 不可续借 ' if delta >= 0: send_info += ' 剩余:' + str(delta) + " 天" else: send_info += ' 超期了' send_info += '\n\n' if send_info: print send_info send_mail(mailto_list, '图书馆借书信息', send_info) #快超期就发邮件提示 else: print "书都好着呢" else: print "可能接口出问题了。。。" else: print "学号或者密码错误" except urllib2.URLError, e: if hasattr(e, "code"): print e.code if hasattr(e, "reason"): print e.reason def send_mail(to_list, sub, content): me = "图书馆借书信息"+"<"+mail_user+">" msg = MIMEText(content, _subtype='plain', _charset='utf-8') msg['Subject'] = sub msg['From'] = me msg['To'] = ";".join(to_list) try: server = smtplib.SMTP() server.connect(mail_host) server.login(mail_user, mail_pass) server.sendmail(me, to_list, msg.as_string()) server.close() print "发送成功" return True except Exception, e: print str(e) print "发送失败" return False if __name__ == "__main__": main()
首先我云主机没钱的,因此crontab
无法用了,哎怎么办呢, 嗯Window计划任务貌似能够,试试。可让脚本在联网的时候就检查一遍。微信
在脚本执行的时候,有的会有黑窗口,类型交互界面同样,不要!cookie
定义触发器,其中10000
表示网络链接的事件ID网络
定义脚本,注意程序默认执行的程序是python.exe
session
在任务网络链接的时候,都记录事件学习