咱們圖書館其實書更新的挺快,各方面都還能夠的,可是沒有過時提醒的功能,致使我屢次超期罰現,很不爽,是時候來個腳本搞定他了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
在任務網絡鏈接的時候,都記錄事件學習