• 【转载】Python实现图书馆预约功能


    注释:

    1,原博主是:http://blog.csdn.net/cq361106306/article/details/42644001#

    2,学校是我现在的学校,我最近也在研究这个,所以转了。

    3,请勿长期使用,容易黑名单。(反正我手动抢也能抢一堆)

    4,仅供学习交流,侵删。

      1 #-*- coding: utf-8 -*-  
      2 #!usr/bin/env python  
      3 import os  
      4 import urllib2  
      5 import urllib  
      6 import cookielib  
      7 import re  
      8 from bs4 import BeautifulSoup  
      9   
     10 Login_URL='http://xxxx'  
     11 Main_URL='http://xxxx/Florms/FormSYS.aspx'  
     12 username='XXXXX'  
     13 password='XXX'  
     14 roomNum='20200xxxx'  
     15 roomId=['309','310','311','307',]  
     16 day='2015-XXXX'  
     17 def login():  
     18     Seats_URL='http://10.21.16.215/FunctionPages/SeatBespeak/BespeakSubmitWindow.aspx?parameters='  
     19     cj = cookielib.CookieJar()  
     20     opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))  
     21     urllib2.install_opener(opener)  
     22     pre_response=opener.open(Login_URL)  
     23     soup=BeautifulSoup(pre_response.read())  
     24     div=soup.find(id='__EVENTVALIDATION')  
     25     attr1=dict(div.attrs)['value']  
     26     #print attr1  
     27     div=soup.find(id='__VIEWSTATE')  
     28     attr2=dict(div.attrs)['value']  
     29     #print attr2  
     30     #login  
     31     LoginData = {  
     32             'txtUserName':username,  
     33             'txtPassword':password,  
     34             '__EVENTVALIDATION':attr1,  
     35             '__VIEWSTATE':attr2,  
     36             'cmdOK.x':'40',  
     37             'cmdOK.y':'14',  
     38             };   
     39     login_req = urllib2.Request(Login_URL, urllib.urlencode(LoginData));  
     40     login_req.add_header('User-Agent', "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36");  
     41     login_response=opener.open(login_req)  
     42     print 'login success'  
     43     #get chairinfo  
     44     while search_seats(day)==False:  
     45         print 'system is not running , waitting '  
     46     while get_start(Seats_URL,opener)==False:  
     47         print 'try again'  
     48     print 'Success and enjoy'  
     49 def get_start(Seats_URL,opener):  
     50     total_chair_args=''  
     51     i=0;  
     52     while total_chair_args=='' and i<len(roomId):  
     53         total_chair_args=get_chair_info(roomId[i])  
     54         print 'start try roomid of '+roomId[i]  
     55         if total_chair_args=='':  
     56             print roomId[i]+' has been ordered
    ';  
     57         i=i+1  
     58     if total_chair_args=='':  
     59         print 'All seats has been ordered'  
     60         return False  
     61     else:  
     62         Seats_URL=Seats_URL+total_chair_args  
     63         print 'starting get seats order info'  
     64         #get order args by header  
     65         headers={  
     66                 #'X-Requested-With': 'XMLHttpRequest',  
     67                 'Accept-Language': 'zh-cn',  
     68                 'Accept-Encoding': 'gzip, deflate',  
     69                 #'ContentType': 'application/x-www-form-urlencoded; charset=UTF-8',  
     70                 'Host': '10.21.16.215',  
     71                 #'Origin':'http://10.21.16.215',  
     72                 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1',    
     73                 'Referer' : 'http://10.21.16.215/FunctionPages/SeatBespeak/BespeakSeatLayout.aspx?roomId='+roomNum,  
     74                 'Connection' : 'Keep-Alive',  
     75         }  
     76         req = urllib2.Request(Seats_URL,headers=headers);  
     77         response=urllib2.urlopen(req)  
     78         result=response.read()  
     79         #print result  
     80         #get order info  
     81         soup=BeautifulSoup(result)  
     82         div=soup.find(id='__EVENTVALIDATION')  
     83         attr1=dict(div.attrs)['value']  
     84         #print attr1  
     85         div=soup.find(id='__VIEWSTATE')  
     86         attr2=dict(div.attrs)['value']  
     87         #print attr2  
     88         #get Seats  
     89         SeatData = {  
     90                 '__EVENTTARGET':'ContentPanel1$btnBespeak',  
     91                 '__EVENTARGUMENT':' ',  
     92                 '__VIEWSTATE':attr2,  
     93                 '__EVENTVALIDATION':attr1,  
     94                 'roomOpenTime':'8:30',  
     95                 'X_CHANGED':'false',  
     96                 'X_TARGET':'ContentPanel1_btnBespeak',  
     97                 'Form2_Collapsed':'false',  
     98                 'ContentPanel1_Collapsed':'false',  
     99                 'X_AJAX':'true',  
    100                 #'X_STATE':'eyJGb3JtMl9jdGwwMF9sYmxSb29tTmFtZSI6eyJUZXh0Ijoi5LiJ5qW85Y2V5Lq656CU5L+u5a6kIn0sIkZvcm0yX2N0bDAxX2xibFNlYXRObyI6eyJUZXh0IjoiMzEzIn0sIkZvcm0yX2N0bDAyX2xibGJlZ2luRGF0ZSI6eyJUZXh0IjoiMjAxNS0xLTEyIn0sIkZvcm0yX2N0bDAzX2xibEVuZERhdGUiOnsiVGV4dCI6Ijc6MDDoh7MxMDowMCJ9fQ==',   
    101                 };   
    102         headers = {  
    103                 'X-Requested-With': 'XMLHttpRequest',  
    104                 'Accept-Language': 'zh-cn',  
    105                 'Accept-Encoding': 'gzip, deflate',  
    106                 'ContentType': 'application/x-www-form-urlencoded; chartset=UTF-8',  
    107                 'Host': '10.21.16.215',  
    108                 'Origin':'http://10.21.16.215',  
    109                 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1',    
    110                 'Referer' :Seats_URL ,  
    111                 'Connection' : 'Keep-Alive'  
    112         }  
    113         result=get_chair_order(SeatData,headers,opener,Seats_URL)  
    114         if result.find('失败')>=0 or get_log()==False:  
    115             return False  
    116         else:  
    117             return True  
    118 #search seats  
    119 def search_seats(datetime):  
    120     postdata={  
    121         '__EVENTTARGET':'Form2$ctl00$btnnewdate',  
    122         '__EVENTARGUMENT':' ',  
    123         '__VIEWSTATE':'/wEPDwUKLTM3MzI4Mzg0MmQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgYFBUZvcm0yBRZGb3JtMiRjdGwwMCRkZGxMaWJyYXJ5BRdGb3JtMiRjdGwwMCRkcFN0YXJ0RGF0ZQUWRm9ybTIkY3RsMDAkYnRubmV3ZGF0ZQUMZ3JpZFJvb21MaXN0BQpXaW5kb3dFZGl05pJoDo97fsj4YPsAfwutGBWhcfnVi5TdDL2VUl7PIH8=',  
    124         'Form2$ctl00$ddlLibrary':'02',  
    125         'Form2$ctl00$dpStartDate':datetime,  
    126         'X_CHANGED':'true',  
    127         'WindowEdit_Hidden':'true',  
    128         'X_TARGET':'Form2_ctl00_btnnewdate',  
    129         'Form2_Collapsed':'false',  
    130         'gridRoomList_Collapsed':'false',  
    131         'gridRoomList_SelectedRowIndexArray':' ',  
    132         'WindowEdit_Collapsed':'false',  
    133         'X_AJAX':'true',  
    134         'X_STATE':'eyJGb3JtMl9jdGwwMF9kZGxMaWJyYXJ5Ijp7IkRhdGFUZXh0RmllbGQiOiJOYW1lIiwiRGF0YVZhbHVlRmllbGQiOiJObyIsIlhfSXRlbXMiOltbIjAxIiwi5rKZ5rKz5qCh5Yy65Zu+5Lmm6aaGIiwxXSxbIjAyIiwi5riF5rC05rKz5qCh5Yy65Zu+5Lmm6aaGIiwxXSxbIjAzIiwi5Zu+5Lmm6aaG5Yib5paw5a6e6aqM5a6kIiwxXV0sIlNlbGVjdGVkVmFsdWUiOiIwMiJ9LCJGb3JtMl9jdGwwMF9kcFN0YXJ0RGF0ZSI6eyJNaW5EYXRlIjoiMjAxNS0wMS0xM1QwMTowNDo1Ni40OTY1KzA4OjAwIiwiVGV4dCI6IjIwMTUtMDEtMTMifSwiZ3JpZFJvb21MaXN0Ijp7IlJlY29yZENvdW50IjozLCJYX1Jvd3MiOnsiVmFsdWVzIjpbWyIyMDIwMDUiLCLkuozmpbzljZXkurrnoJTkv67lrqQiLCLmuIXmsLTmsrPmoKHljLrlm77kuabppoYiLCIyMiIsIjIyIiwiPGEgZXh0OnF0aXA9XCLpooTnuqbml7bpl7TkuLrvvJoxMzowMOWIsDIzOjU5XCIgIGNsYXNzPVwieC1pdGVtLWRpc2FibGVkXCIgZGlzYWJsZWQ9XCJkaXNhYmxlZFwiPjxpbWcgc3JjPVwiL0ltYWdlcy9pY29uL2J1bGxldF9jcm9zcy5wbmdcIiAgZXh0OnF0aXA9XCLpooTnuqbml7bpl7TkuLrvvJoxMzowMOWIsDIzOjU5XCIgIC8+PC9hPiJdLFsiMjAyMDA2Iiwi5LiJ5qW85Y2V5Lq656CU5L+u5a6kIiwi5riF5rC05rKz5qCh5Yy65Zu+5Lmm6aaGIiwiMjQiLCIyNCIsIjxhIGV4dDpxdGlwPVwi6aKE57qm5pe26Ze05Li677yaMTM6MDDliLAyMzo1OVwiICBjbGFzcz1cIngtaXRlbS1kaXNhYmxlZFwiIGRpc2FibGVkPVwiZGlzYWJsZWRcIj48aW1nIHNyYz1cIi9JbWFnZXMvaWNvbi9idWxsZXRfY3Jvc3MucG5nXCIgIGV4dDpxdGlwPVwi6aKE57qm5pe26Ze05Li677yaMTM6MDDliLAyMzo1OVwiICAvPjwvYT4iXSxbIjIwMjAwNyIsIuWbm+alvOWNleS6uueglOS/ruWupCIsIua4heawtOays+agoeWMuuWbvuS5pummhiIsIjQiLCI0IiwiPGEgZXh0OnF0aXA9XCLpooTnuqbml7bpl7TkuLrvvJoxMzowMOWIsDIzOjU5XCIgIGNsYXNzPVwieC1pdGVtLWRpc2FibGVkXCIgZGlzYWJsZWQ9XCJkaXNhYmxlZFwiPjxpbWcgc3JjPVwiL0ltYWdlcy9pY29uL2J1bGxldF9jcm9zcy5wbmdcIiAgZXh0OnF0aXA9XCLpooTnuqbml7bpl7TkuLrvvJoxMzowMOWIsDIzOjU5XCIgIC8+PC9hPiJdXSwiRGF0YUtleXMiOltbbnVsbF0sW251bGxdLFtudWxsXV0sIlN0YXRlcyI6W1tdLFtdLFtdXX19fQ==',  
    135     }  
    136     headers={  
    137             'X-Requested-With': 'XMLHttpRequest',  
    138             'Accept-Language': 'zh-cn',  
    139             'Accept-Encoding': 'gzip, deflate',  
    140             'ContentType': 'application/x-www-form-urlencoded; charset=UTF-8',  
    141             'Host': '10.21.16.215',  
    142             'Origin':'http://10.21.16.215',  
    143             'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1',    
    144             'Referer' : 'http://10.21.16.215/FunctionPages/SeatBespeak/BespeakSeat.aspx',  
    145             'Connection' : 'Keep-Alive',  
    146     }  
    147     req = urllib2.Request('http://10.21.16.215/FunctionPages/SeatBespeak/BespeakSeat.aspx', urllib.urlencode(postdata),headers);  
    148     response=urllib2.urlopen(req)  
    149     result=response.read()  
    150     if result.find('zoom.png')>=0:  
    151         return True  
    152     else:  
    153         return False  
    154     #out=open('tt.html','w')  
    155     #out.write(result)  
    156 #get chair location args  
    157 def get_chair_info(roomId_):  
    158     print 'start get seats info'  
    159     room_url='http://10.21.16.215/FunctionPages/SeatBespeak/SeatLayoutHandle.ashx'  
    160     #datetime=raw_input('input your date of tomorrw(2015-01-13):  ')  
    161     datetime=day  
    162     datetime=datetime+' 0:00:00'  
    163     print datetime  
    164     postdata={  
    165         'roomNum':roomNum,  
    166         'date':datetime,  
    167         'divTransparentTop':'0',  
    168         'divTransparentLeft':'0'  
    169     }  
    170     headers={  
    171             'X-Requested-With': 'XMLHttpRequest',  
    172             'Accept-Language': 'zh-cn',  
    173             'Accept-Encoding': 'gzip, deflate',  
    174             'ContentType': 'application/x-www-form-urlencoded;',  
    175             'Host': '10.21.16.215',  
    176             'Origin':'http://10.21.16.215',  
    177             'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1',    
    178             'Referer' : 'http://10.21.16.215/FunctionPages/SeatBespeak/BespeakSeatLayout.aspx?roomId='+roomNum,  
    179             'Connection' : 'Keep-Alive',  
    180     }  
    181     req = urllib2.Request(room_url, urllib.urlencode(postdata),headers);  
    182     response=urllib2.urlopen(req)  
    183     result=response.read()  
    184     out=open('info.html','w')  
    185     out.write(result)  
    186     soup=BeautifulSoup(result)  
    187     #roomId  
    188     div=soup.find(id=roomNum+roomId_)  
    189     chair_info=dict(div.attrs)['onclick']  
    190     #print chair_info  
    191     matchObj=re.search(  r'BespeakSeatClick(.∗?)', chair_info, re.M|re.I)  
    192     chair_args=matchObj.group(1)[1:-1]  
    193     #print chair_args  
    194     return chair_args  
    195     #print result  
    196 #get order  
    197 def get_chair_order(SeatData,headers,opener,Seats_URL):  
    198     seat_req = urllib2.Request(Seats_URL, urllib.urlencode(SeatData),headers);  
    199     seat_response=opener.open(seat_req)  
    200     result=seat_response.read()  
    201     return result  
    202 #get chair log  
    203 def get_log():  
    204     url='http://10.21.16.215/FunctionPages/ReaderLog/SelectBespeakLog.aspx';  
    205     headers={  
    206             #'X-Requested-With': 'XMLHttpRequest',  
    207             'Accept-Language': 'zh-cn',  
    208             'Accept-Encoding': 'gzip, deflate',  
    209             #'ContentType': 'application/x-www-form-urlencoded; charset=UTF-8',  
    210             'Host': '10.21.16.215',  
    211             #'Origin':'http://10.21.16.215',  
    212             'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1',    
    213             'Referer' : 'http://10.21.16.215/Florms/FormSYS.aspx',  
    214             'Connection' : 'Keep-Alive',  
    215     }  
    216     req = urllib2.Request(url,headers=headers);  
    217     response=urllib2.urlopen(req)  
    218     result=response.read()  
    219     #line = '三楼单人研修室", "304", "已确认", "15-01-10 13:00:18", "15-01-11 08:30:00", "", "在'  
    220     #print result.find('三楼单人研修室')  
    221     #matchObj=re.search(  r'三楼单人研修室(.*?)08:30:00', result, re.M|re.I)  
    222     #print matchObj.group(1)  
    223     p=re.compile(r'"三楼单人研修室"(.*?)08:30:00')  
    224     #print p.findall(result)[-1]  
    225     flag=p.findall(result)[-1].find('已取消')  
    226     if flag>=0:  
    227         return False  
    228     else:  
    229         return True  
    230   
    231 if __name__=='__main__':  
    232     login()  
  • 相关阅读:
    结对项目 sport club(一)
    结对项目 sport club(三)
    结对项目 sport club(二)
    博客作业
    学生信息管理系统APP需求分析
    随机生成四则运算
    软件介绍
    利用纯css写三角形,弧度箭头,吃豆人,气泡。放大镜,标签的源码
    js中的数据类型隐式转换的三种情况
    javascript基础入门之js中的结构分支与循环语句
  • 原文地址:https://www.cnblogs.com/ldzhangyx/p/6140945.html
Copyright © 2020-2023  润新知