代码仅供测试方便所用。
这个图书管理系统是asp.net EXTJS写的 在分析页面的时候非常复杂,一度差点放弃。不过还是慢慢的进步。还顺便学了一下正则表达式。这个代码的作用就是登陆进系统,然后获取预约信息。代码有点乱,以后在整理,这里先存档
这个网址校外ip不能访问,所以代码仅供参考,部分逻辑还是能够套用的
#-*- coding: utf-8 -*- #!usr/bin/env python import os import urllib2 import urllib import cookielib import re from bs4 import BeautifulSoup Login_URL='http://10.21.16.215' Main_URL='http://10.21.16.215/Florms/FormSYS.aspx' Seats_URL='http://10.21.16.215/FunctionPages/SeatBespeak/BespeakSubmitWindow.aspx?parameters=89B0F70D378F61A7BA0411B5439DBC76521E906832306CE419BCCD5F85380BE374557D246EE19799E9A8992E242BEC3261F078093CE5602B53E9BE3DD0AD5696E85B6BEECD6835BA' username='XXXX' password='XXXX' roomNum='202006' def login(): cj = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) urllib2.install_opener(opener) pre_response=opener.open(Login_URL) soup=BeautifulSoup(pre_response.read()) div=soup.find(id='__EVENTVALIDATION') attr1=dict(div.attrs)['value'] #print attr1 div=soup.find(id='__VIEWSTATE') attr2=dict(div.attrs)['value'] #print attr2 #login LoginData = { 'txtUserName':username, 'txtPassword':password, '__EVENTVALIDATION':attr1, '__VIEWSTATE':attr2, 'cmdOK.x':'40', 'cmdOK.y':'14', }; login_req = urllib2.Request(Login_URL, urllib.urlencode(LoginData)); 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"); login_response=opener.open(login_req) print 'login success' soup=BeautifulSoup(opener.open(Main_URL).read()) div=soup.find(id='__VIEWSTATE') viewstate=dict(div.attrs)['value'] print 'start get seats info' room_url='http://10.21.16.215/FunctionPages/SeatBespeak/SeatLayoutHandle.ashx' #datetime=raw_input('input your date of tomorrw(2015-01-13): ') datetime='2015-01-13' datetime=datetime+' 0:00:00' print datetime postdata={ 'roomNum':'202006', 'date':datetime, 'divTransparentTop':'0', 'divTransparentLeft':'0' } headers={ 'X-Requested-With': 'XMLHttpRequest', 'Accept-Language': 'zh-cn', 'Accept-Encoding': 'gzip, deflate', 'ContentType': 'application/x-www-form-urlencoded;', 'Host': '10.21.16.215', 'Origin':'http://10.21.16.215', 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1', 'Referer' : 'http://10.21.16.215/FunctionPages/SeatBespeak/BespeakSeatLayout.aspx?roomId=202006&date=635566176000000000', 'Connection' : 'Keep-Alive', } #req = urllib2.Request(room_url, urllib.urlencode(postdata),headers); #response=opener.open(req) #result=response.read() #print result #get Seats SeatData = { '__EVENTTARGET':'ContentPanel1$btnBespeak', '__EVENTARGUMENT':' ', '__VIEWSTATE':'/wEPDwULLTEzMzAxMjU4NDFkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYEBQVGb3JtMgUNQ29udGVudFBhbmVsMQUYQ29udGVudFBhbmVsMSRidG5CZXNwZWFrBRZDb250ZW50UGFuZWwxJGJ0bkNsb3NlFnml1ZniYWarY7w+39y6XMauzvQuUhZHmd7FIJHtCzY=', '__EVENTVALIDATION':'/wEWAgKz1fTFAgL+mI+WBmMYZmWmY/5C6T+zShCagVwIeSBhVaQY5pYrGTq0aLBk', 'roomOpenTime':'8:30', 'X_CHANGED':'false', 'X_TARGET':'ContentPanel1_btnBespeak', 'Form2_Collapsed':'false', 'ContentPanel1_Collapsed':'false', 'X_AJAX':'true', #'X_STATE':'eyJGb3JtMl9jdGwwMF9sYmxSb29tTmFtZSI6eyJUZXh0Ijoi5LiJ5qW85Y2V5Lq656CU5L+u5a6kIn0sIkZvcm0yX2N0bDAxX2xibFNlYXRObyI6eyJUZXh0IjoiMzEzIn0sIkZvcm0yX2N0bDAyX2xibGJlZ2luRGF0ZSI6eyJUZXh0IjoiMjAxNS0xLTEyIn0sIkZvcm0yX2N0bDAzX2xibEVuZERhdGUiOnsiVGV4dCI6Ijc6MDDoh7MxMDowMCJ9fQ==', }; headers = { 'X-Requested-With': 'XMLHttpRequest', 'Accept-Language': 'zh-cn', 'Accept-Encoding': 'gzip, deflate', 'ContentType': 'application/x-www-form-urlencoded; chartset=UTF-8', 'Host': '10.21.16.215', 'Origin':'http://10.21.16.215', 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1', 'Referer' : 'http://10.21.16.215/FunctionPages/SeatBespeak/BespeakSubmitWindow.aspx?parameters=89B0F70D378F61A7BA0411B5439DBC76521E906832306CE419BCCD5F85380BE374557D246EE19799E9A8992E242BEC3261F078093CE5602B53E9BE3DD0AD5696E85B6BEECD6835BA', 'Connection' : 'Keep-Alive' } ''' loc=1 while loc>=0: result=get_chair(SeatData,headers,opener) loc=result.find('失败') print 'failed and try' ''' print 'Get your chair ,Success!!!' #send qing shui he ''' postdata={ '__EVENTTARGET':'Form2$ctl00$btnnewdate', '__EVENTARGUMENT':' ', '__VIEWSTATE':'/wEPDwUKLTM3MzI4Mzg0MmQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgYFBUZvcm0yBRZGb3JtMiRjdGwwMCRkZGxMaWJyYXJ5BRdGb3JtMiRjdGwwMCRkcFN0YXJ0RGF0ZQUWRm9ybTIkY3RsMDAkYnRubmV3ZGF0ZQUMZ3JpZFJvb21MaXN0BQpXaW5kb3dFZGl05pJoDo97fsj4YPsAfwutGBWhcfnVi5TdDL2VUl7PIH8=', 'Form2$ctl00$ddlLibrary':'02', 'Form2$ctl00$dpStartDate':'2015-01-12', 'X_CHANGED':'true', 'WindowEdit_Hidden':'true', 'X_TARGET':'Form2_ctl00_btnnewdate', 'Form2_Collapsed':'false', 'gridRoomList_Collapsed':'false', 'gridRoomList_SelectedRowIndexArray':' ', 'WindowEdit_Collapsed':'false', 'X_AJAX':'true', 'X_STATE':'eyJGb3JtMl9jdG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NyYz1cIi9JbWFnZXMvaWNvbi9idWxsZXRfY3Jvc3MucG5nXCIgIGV4dDpxdGlwPVwi6aKE57qm5pe26Ze05Li677yaMTM6MDDliLAyMzo1OVwiICAvPjwvYT4iXSxbIjIwMjAwNyIsIuWbm+alvOWNleS6uueglOS/ruWupCIsIua4heawtOays+agoeWMuuWbvuS5pummhiIsIjQiLCI0IiwiPGEgZXh0OnF0aXA9XCLpooTnuqbml7bpl7TkuLrvvJoxMzowMOWIsDIzOjU5XCIgIGNsYXNzPVwieC1pdGVtLWRpc2FibGVkXCIgZGlzYWJsZWQ9XCJkaXNhYmxlZFwiPjxpbWcgc3JjPVwiL0ltYWdlcy9pY29uL2J1bGxldF9jcm9zcy5wbmdcIiAgZXh0OnF0aXA9XCLpooTnuqbml7bpl7TkuLrvvJoxMzowMOWIsDIzOjU5XCIgIC8+PC9hPiJdXSwiRGF0YUtleXMiOltbbnVsbF0sW251bGxdLFtudWxsXV0sIlN0YXRlcyI6W1tdLFtdLFtdXX19fQ==', } headers={ 'X-Requested-With': 'XMLHttpRequest', 'Accept-Language': 'zh-cn', 'Accept-Encoding': 'gzip, deflate', 'ContentType': 'application/x-www-form-urlencoded; charset=UTF-8', 'Host': '10.21.16.215', 'Origin':'http://10.21.16.215', 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1', 'Referer' : 'http://10.21.16.215/FunctionPages/SeatBespeak/BespeakSeat.aspx', 'Connection' : 'Keep-Alive', } req = urllib2.Request('http://10.21.16.215/FunctionPages/SeatBespeak/BespeakSeat.aspx', urllib.urlencode(postdata),headers); response=opener.open(req) result=response.read() print result # first page headers={ #'X-Requested-With': 'XMLHttpRequest', 'Accept-Language': 'zh-cn', 'Accept-Encoding': 'gzip, deflate', #'ContentType': 'application/x-www-form-urlencoded; charset=UTF-8', 'Host': '10.21.16.215', #'Origin':'http://10.21.16.215', 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1', 'Referer' : 'http://10.21.16.215/Florms/FormSYS.aspx', 'Connection' : 'Keep-Alive', } req = urllib2.Request('http://10.21.16.215/FunctionPages/SeatBespeak/BespeakSeat.aspx',headers=headers); response=opener.open(req) result=response.read() #print result #print result ''' print get_log() def get_chair(SeatData,headers,opener): seat_req = urllib2.Request(Seats_URL, urllib.urlencode(SeatData),headers); seat_response=opener.open(seat_req) result=seat_response.read() return result #get chair log def get_log(): url='http://10.21.16.215/FunctionPages/ReaderLog/SelectBespeakLog.aspx'; headers={ #'X-Requested-With': 'XMLHttpRequest', 'Accept-Language': 'zh-cn', 'Accept-Encoding': 'gzip, deflate', #'ContentType': 'application/x-www-form-urlencoded; charset=UTF-8', 'Host': '10.21.16.215', #'Origin':'http://10.21.16.215', 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1', 'Referer' : 'http://10.21.16.215/Florms/FormSYS.aspx', 'Connection' : 'Keep-Alive', } req = urllib2.Request(url,headers=headers); response=urllib2.urlopen(req) result=response.read() #line = '三楼单人研修室", "304", "已确认", "15-01-10 13:00:18", "15-01-11 08:30:00", "", "在' #print result.find('三楼单人研修室') #matchObj=re.search( r'三楼单人研修室(.*?)08:30:00', result, re.M|re.I) #print matchObj.group(1) p=re.compile(r'"三楼单人研修室"(.*?)08:30:00') #print p.findall(result)[-1] flag=p.findall(result)[-1].find('已取消') if flag>=0: return True else: return False if __name__=='__main__': login()
时间: 2024-10-18 14:41:22