An oriental fashion (Driving School) course competition program implemented in python, python Oriental fashion
2014-10-23
I decided to write a ticket snatching program to help me brush the ticket because I had too many resources.
Step 1: capture packets. Log on to the Course Selection System in the browser and use the packet capture tool to observe network behavior. Here I use fildder. Packet Capture mainly requires two pieces of information. One is the url request sent by the browser, and the other is the header of the http message and the data content and format of the post.
Step 2: Simulate logon. Copy the captured header to simulate a python browser and enter the user name and password. Here, we can verify the code by hand. Let pythonsave the obtained .jpg file to the local device, and then pause waiting for the input. After the human eyes recognize the verification code, they will input it to the program for further execution. At first, I always said that the verification code is incorrect. At last, I found that the request method was incorrect. Here, we should note that each url is obtained using the same opener, so that the server will regard it as the same browser. After modification, the logon is successful.
Step 3: Solve the verification code. Manually entering the verification code is always a long journey, but you still need to let the machine do it. Here we use the PIL package and pytesser package. There is a method to parse the Verification Code that has been implemented, and you just need to use it directly. Because the verification code recognition rate of this package is not 100%, I put the login code into a while loop until the login succeeds.
Step 4: Skip the course. Continue to capture packets, analyze the ur request process of the Course Selection, and then let the program simulate. For example, if the browser finds that there is a course to be selected on Friday evening, and then asks the program to take the course on Friday evening, the program returns the result and displays OK. Refresh the browser. This course is indeed selected, it means the program is finished!
Read data later. The producers of Oriental fashion websites are not completely vegetarian. Because no course can be selected for a good period of time, I asked my program to do the while loop and kept learning about the course selection. After several hours of refreshing, the webpage says that I have performed too many operations and my access is prohibited today. In order to solve this problem, I changed the frequency of class refresh to 10 minutes. Because the course also requires a verification code, but the verification code recognition rate is not high, it would be a waste of time to wait for 10 minutes because of a wrong verification code, so I changed the program to no class, wait for 10 minutes, and Keep refreshing if you have a class. In this way, you need to extract data, analyze the webpage structure, and find that a separate url uses the stored data, and the remaining lesson is in a json string. Extract the string using regular expression matching and parse the json data to obtain the required data!
Finally, paste my code:
1 import re 2 import json 3 import time 4 import urllib 5 import urllib2 6 import urlparse 7 import cookielib 8 from PIL import Image, ImageDraw, ImageFont, imageFilter 9 from pytesser import * 10 from datetime import date 11 import OS 12 13 OS. chdir ('C: // Python27/Lib/site-packages/pytesser ') 14 15 def getVerify (name): 16 # data = urllib2.urlopen (17 im = Image. open (name) 18 imgry = im. convert ('l ') 19 text = image_to_string (imgry) 20 text = re. sub ('\ W', '', text) 21 return text 22 23 def urlToString (url): 24 data = urllib2.urlopen (url ). read () 25 f = open ('buffer/temp.jpg ', 'wb') 26 f. write (data) 27 f. close () 28 return getVerify ('buffer/temp.jpg ') 29 30 def openerUrlToString (opener, url): 31 data = opener. open (url ). read () 32 f = open ('buffer/temp.jpg ', 'wb') 33 f. write (data) 34 f. close () 35 ret Urn getVerify ('buffer/temp.jpg ') 36 37 def getOpener (head): 38 # deal with the Cookies 39 cj = cookielib. cookieJar () 40 pro = urllib2.HTTPCookieProcessor (cj) 41 opener = urllib2.build _ opener (pro) 42 header = [] 43 for key, value in head. items (): 44 elem = (key, value) 45 header. append (elem) 46 opener. addheaders = header 47 return opener 48 49 def decodeAnyType (data): 50 ret = data 51 try: 52 temp = Data. decode ('utf-8') 53 ret = temp 54 failed T: 55 pass 56 try: 57 temp = data. decode ('gbk') 58 ret = temp 59 failed T: 60 pass 61 try: 62 temp = data. decode ('gb2312') 63 ret = temp 64 failed T: 65 pass 66 return ret 67 68 header = {69 'connection': 'Keep-alive', 70' Accept ': 'text/html, application/xhtml + xml, */* ', 71 'Accept-color': 'en-US, en; q = 0.8, zh-Hans-CN; q = 0.5, zh-Hans; q = 0.3 ', 72 'user-Agent ': 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv: 11.0) like Gecko', 73 'Accept-encoding': 'gzip, deflate ', 74 'host': 'wsyc .dfss.com.cn ', 75 'dnt ': '1' 76} 77 78 # the data below are settled by customer to select the class needed 79 start = 13 80 end = 17 81 numid = '3' 82 year = 2014 83 month = 12 84 day = 22 85 username = '000000' 86 password = '000000' 87 88 opener = getOpener (header) 89 url1 = 'HTTP: // wsyc.dfss.com.cn/'90 url2 = 'HTTP: // 41591 url3 = 'HTTP: // wsyc.dfss.com.cn/validpng.aspx? Aa = 3 & page = lg '92 url4 = 'HTTP: // wsyc.dfss.com.cn/pc-client/jbxx.aspx' 93 url5 = 'HTTP: // reject 94 95 # try to login until the validcode is right 96 count = 0 97 while True: 98 print '------------------------ '99 print 'have tryed to login % d times, now try again! '% (Count) 100 count = count + 1101 validcode = openerUrlToString (opener, url3) 102 print 'the validcode is' + validcode103 postDict = {104 'ajaxmethod': 'login ', 105 'account': username, 106 'validcode': ValidCode, 107 'pwd': password108} 109 110 postData = urllib. urlencode (postDict ). encode () 111 op = opener. open( url2, postData) 112 result = op. read (). decode ('utf-8') 113 print 'the result of login is' + Result114 # if result. find ('true')> = 0: 115 if result = 'true': 116 print 'login success! '1970 break118 else: 117 continue120 119 121 yuechedate = date (year, month, day) 122 today = date. today () 124 intervaldays = (yuechedate-today ). days125 print intervaldays126 if intervaldays <exit () 128 validcode = ''129 count = 0130 # try to select a class until success131 while True: 132 print '-------------------------- '133 print' have tryed to select % d times, now try again! '% (Count) 134 count = count + 1135 try: 136 validcode = openerUrlToString (opener, url5) 137 bytes T: 138 continue139 url7 = 'HTTP: // wsyc.dfss.com.cn/Ajax/StuHdl.ashx? LoginType = 2 & method = stu '\ 140 +' & stuid = % s & sfznum = & carid = & ValidCode = % s' % (username, validcode) 141 data = opener. open (url7 ). read (). decode ('utf-8') 142 strs = re. search ('\ [\ {\ "fchrdate. *? \}\] ', Data) 143 # print data144 print strs145 if strs is None: 146 continue147 jsontext = json. loads (strs. group () 148 num = jsontext [intervaldays] [numid]. split ('/') [1] 149 print 'remain num is '+ num150 if num = '0': 151 print' no class avaliable! '1970 time. sleep (152) 600 continue154 try: 153 validcode = openerUrlToString (opener, url5) 155 TB: 156 continue158 url6 = 'HTTP: // response? LoginType = 2 & method = yueche '\ 159 +' & stuid = % s & bmnum = BD14101500687 & start = % d & end = % d' % (username, start, end) \ 160 + '& lessionid = 001 & trainpriceid = BD13040300001 & lesstypeid = 02' \ 161 + '& date = % d-% d' % (year, month, day) \ 162 + '& id = 1 & carid = & ycmethod = 03 & cartypeid = 01 & trainsessionid = 0' + numid \ 163 +' & ReleaseCarID = & ValidCode = '+ validcode164 result = opener. open (url6 ). read (). decode ('utf-8') 165 print 'result Of select is '+ result166 if result = 'success': 167 print 'select success! '1970 break169 else: 168 continueView Code