Python automatic 12306 ticketing software implementation code, python12306 Ticketing

Source: Internet
Author: User

Python automatic 12306 ticketing software implementation code, python12306 Ticketing

Yesterday I sent the captured 12306 data packets and analyzed them. Today, according to yesterday's analysis, I implemented the data using code. If you need the data, you can check that the implemented functions include: log on, the verification code is recognized and the ticket is checked automatically. If you have more than one ticket, click the reservation. The last step is poor. You can study it yourself.

Import requestsimport timeimport dmptimport reimport randomfrom copyheaders import handler = {'host': 'kyfw .12306.cn ', 'connection': 'Keep-alive', 'upgrade-Insecure-requests ': '1', 'user-agent': 'mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/8080 ', 'accept': 'text/html, application/xhtml + xml, application/xml; q = 0.9, image/webp, I Mage/apng, */*; q = 0.8 ', 'Referer': 'https: // your get_random (): return str (random. random () # generate an 18-bit random number def get_13_time (): # return str (int (time. time () * 1000) class CN12306 (object): def _ init _ (self): self. chufa = '2017-02-03 'self. s = requests. session () self. s. verify = False # ignore https certificate verification def get_init (self): # request a homepage url = 'https: // k Yf201712306.cn/otn/login/init'r = self. s. get (url) print ('homepage retrieved successfully, status code: ', r) def get_newpasscode (self): # This page does not know what it is, but 12306 requests are sent, we also request url = 'https: // kyf201712306.cn/otn/resources/js/newpasscode/captcha_js.js to imitate it? _ = {}'. Format (get_13_time () r = self. s. get (url) print ('newpasscode obtained successfully, status code: ', r) def get_auth_code (self): # obtain the verification code url = 'https: // kyf201712306.cn/passport/captcha/captcha-image? Login_site = E & module = login & rand = sjrand &{}'. format (get_random () r = self. s. get (url) with open('auth_code.png ', 'wb') as auth: auth. write (r. content) auth. flush () auth. close () return str (r) = '<Response [200]> 'def analysis_auth_code (self): # Call the CAPTCHA human bypass platform and return the verification code coordinate dmt = dmpt. damatuApi ("CAPTCHA human bypass platform account", "CAPTCHA human bypass platform password") analysis_auth = dmt.decode('auth_code.png ', 287) # upload the CAPTCHA human bypass analysis_auth = re. sub ('\ |', analysis_auth) l I = analysis_auth.split (',') for I in range (len (li): if I % 2 = 0: pass else: li [I] = str (int (li [I])-30) analysis_auth = str (li) analysis_auth = re. sub ("'", '', analysis_auth) analysis_auth = re. sub ("\]", '', analysis_auth) analysis_auth = re. sub ("\ [", '', analysis_auth) analysis_auth = re. sub ("", '', analysis_auth) print ('verification code coordinate ', analysis_auth) self. analysis_auth = analysis_auth # Verification Code coordinate def Uth_auth_code (self): # verify whether the verification code is submitted correctly. post url = 'https: // kyf201712306.cn/passport/captcha/captcha-check' data = {'answer': self. analysis_auth, 'login _ site': 'E', 'rand': 'sjrand',} r = self. s. post (url = url, data = data) print (r. text) if r. text = ''' {"result_message": "Verification Code Verification Successful", "result_code": "4"} ''': # return True else: # print ('verification code error, refresh verification code, and resubmit ') if self. get_auth_code (): # obtain the verification code self. analysis_aut H_code () # Call the human bypass Platform self. auth_auth_code () # re-verify the verification code def login (self): url = 'https: // kyf201712306.cn/passport/web/login'data = {'username': '123', 'Password ': 'password 12306 ', 'appid': 'otn',} r = self. s. post (url = url, data = data) self. uamtk = r. json () ["uamtk"] print (r. text) def userLogin (self): url = 'https: // kyf000012306.cn/otn/login/userlogin' r = self. s. post (url = url) # print (r. text) def getjs (self): # I don't know what it is, but I also mention Submit url = 'https: // kyf201712306.cn/otn/httpzf/getjs' r = self. s. get (url) def post_uamtk (self): url = 'https: // kyf201712306.cn/passport/web/auth/uamtk'data = {'appid ': 'otn'} r = self. s. post (url = url, data = data, allow_redirects = False) self. newapptk = r. json () ["newapptk"] r. encoding = 'utf-8' print (r. text) def post_uamauthclient (self): url = 'https: // kyf201712306.cn/otn/uamauthclient' data = {'tk ': self. newapptk} r = self. s. po St (url = url, data = data) self. apptk = r. json () ["apptk"] r. encoding = 'utf-8' print (r. text) def get_userLogin (self): url = 'https: // kyf201712306.cn/otn/login/userlogin' r = self. s. get (url) r. encoding = 'utf-8' # print (r. text) def get_leftTicket (self): url = 'https: // kyf000012306.cn/otn/leftticket/init' r = self. s. get (url) r. encoding = 'utf-8' # print (r. text) def get_GetJS (self): url = 'https: // kyf201712306.cn/otn/httpzf/ge TJS 'self. s. get (url) def get_qufzjql (self): url = 'https: // kyf201712306.cn/otn/dynamicjs/qufzjql' self. s. get (url) def get_queryZ (self): url = 'https: // kyf201712306.cn/otn/leftticket/queryz? LeftTicketDTO. train_date ={} & leftTicketDTO. from_station ={}& leftTicketDTO. to_station ={}& purpose_codes = {}'. format (self. chufa, 'bjp ', 'tbp', 'adresult') r = self. s. get (url) r. encoding = 'utf-8' # print (r. text) cheliang = r. json () ["data"] ["result"] for I in cheliang: dandulist = str (I ). split ('|') if len (str (dandulist [0]) >=100: self. secretStr = dandulist [0] # secretStr = str (x [0]) Train COUNT = str (dandulist [3]) departure time = str (dandulis T [8]) arrival time = str (dandulist [9]) Duration = str (dandulist [10]) soft sleeper = str (dandulist [23]) hard sleeper = str (dandulist [28]) print (I) print ('list of available taxi times, ', 'train times:', train times, 'departure time: ', departure time, 'arrival time: ', arrival time, 'duration:', duration, 'soft sleeper available: ', soft sleeper, 'hard sleeper available:', hard sleeper) if (soft sleeper! = ''And soft sleeper! = '0' and soft sleeper! = 'None' and soft sleeper! = 'Null') or (hard sleeper! = ''And hard sleeper! = '0' and hard sleeper! = 'None' and hard sleeper! = 'Null'): # execute the order operation self. post_submitOrderRequest () self. post_initDc () self. post_getPassengerDTOs () return False print ('*********************************** ******************') return True # click the pre-order def post_submitOrderRequest (self): url = 'https: // kyf201712306.cn/otn/leftticket/submitorderrequest' data = {'secretstr ': self. secretStr, 'train _ date': self. chufa, # departure time 'back _ train_date ': self. chufa, # return time 'tour _ flag ': 'Dc', 'purpose _ codes': 'Adult ', 'query _ from_station_name': 'beijing', 'query _ to_station_name ': 'tianjin bei ', 'undefined': ''} r = self. s. post (url = url, data = data) print (r. text) def post_initDc (self): url = 'https: // kyf201712306.cn/otn/confirmpassenger/initdc' r = self. s. post (url) # r. text self. REPEAT_SUBMIT_TOKEN = re. findall ("globalRepeatSubmitToken = '(. *?) '; ", R. text) [0] def post_getPassengerDTOs (self): # obtain the passenger information url = 'https: // response data = {'Repeat _ SUBMIT_TOKEN ': self. REPEAT_SUBMIT_TOKEN, '_ json_att': ''} r = self. s. post (url = url, data = data) r. encoding = 'utf-8' print (r. text) if _ name _ = '_ main _': print (get_random () cn = CN12306 () cn. get_init () cn. get_newpasscode () if cn. get_auth_code (): # If the verification code is obtained successfully, call the print ('verification code obtained successfully') print ('calling the CAPTCHA human bypass platform... ') cn. analysis_auth_code () if cn. auth_auth_code (): # verify that the verification code is correct. cn. login () cn. userLogin () cn. getjs () cn. post_uamtk () cn. post_uamauthclient () cn. get_userLogin () cn. get_leftTicket () cn. get_GetJS () cn. get_qufzjql () while cn. get_queryZ (): time. sleep (30)

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.