Python implements the 12306 login function instance code, python12306
The following code provides python 12306 logon. The specific code is as follows:
#! /Usr/bin/env pythonimport requestsimport urllib. parseimport randomimport timereq = requests. session () import sysimport reimport urllib3import getpass # Enter urllib3.disable _ warnings () in the ciphertext field # log on to ----------------------- apptklist = [] newStrList = [] def signin. uniform (1, 0) imgUrl = ('https: // kyf201712306.cn/passport/captcha/captcha-image? ''Login _ site = E & module = login & rand = sjrand & % s' % ran) print (imgUrl) imgresponse = req. get (url = imgUrl, verify = False) codeimg = imgresponse. content fn = open('code.png ', 'wb') fn. write (codeimg) fn. close () codeStr = input ('Enter the coordinates of the Verification Code :') a = ''B = ''c = ''d = ''e = ''f ='' g = ''h = ''if '1' in codeStr: a = '37, 37, 'if' 2' in codeStr: B = '2017, 37, 'if' 3' in codeStr: c = '2017, 37, 'If '4' in codeSt R: d = '2014, 37, 'If '5' in codeStr: e = '2014, 'if '6' in codeStr: f = '2014, 250, 'If '7' in codeStr: g = '2017, 'If '8' in codeStr: h = '2017, 'newcodestr = a + B + c + d + e + f + g + h newStr = newCodeStr [:-1] newStrList. append (newStr) url = 'https: // kyf201712306.cn/passport/captcha/captcha-check' data = {'answer': newStr, 'login _ site': 'E', 'rand ': 'sjrand'} headers = {'user-agent': 'mozilla/5.0 (Windo Ws NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ''chrome/63.0.3239.108 Safari/100',} response = req. post (url = url, data = data, headers = headers, verify = False) print ('detect image ----------- ', url) print (response. text) result = response. json () if result ['result _ Code'] = '4': print ('verification code verified successfully') else: print ('verification code verification failed, be sure to fill in the correct coordinates ') signin () return userName = input ('Please input your userName :') # Password = input ('Please input your password: ') password = getpass. getpass ('Please input your password: ') loginData = {'username': username, 'Password': password, 'appid': 'otn'} headers = {'host ': 'kyfw .12306.cn ', 'Referer': 'https: // kyf201712306.cn/otn/login/init', 'user-agent': 'mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) ''applewebkit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 ''Safari /537.36 '} response = req. post ('https: // your, data = loginData, headers = headers, verify = False) print ('-------------- logging in -------------------- ') print ('https: // your ') print ('response', response. text. encode ('utf-8 '). decode ('utf-8') fd = open ("locate.html", 'wb + ') fd. write (response. content) yzData = {'appid ': 'otn'} response = req. post ('https: // k Yf201712306.cn/passport/web/auth/uamtk', data = yzData, headers = headers, verify = False) print ('--------------------- first verification ------------------- ') print (response. text) # print ('typeof response', type (response) loginMessage = response. json () ['newapptk '] print ('loginmessage =', loginMessage) # Start of the second verification: ++ yz2Data = {'tk ': loginMessage} response = req. post ('https: // kyf201712306.cn/o Tn/uamauthclient ', data = yz2Data, headers = headers, verify = False) print (' --------------------- second verification ------------------- ') print (response. text) apptk = response. json () ['apptk '] apptklist. append (apptk) def buy (): print (newStrList) req. headers ['Referer'] = 'https: // kyf201712306.cn/otn/leftticket/init' result = req. post ('https: // kyf201712306.cn/otn/login/checkuser') print ('-------------- ticket purchasing system --------------') Print (result. text) print ('login Verification Successful checkuser') headers = {'Referer': 'https: // kyf201712306.cn/otn/leftticket/init', 'user-agent ': 'mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/100' (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/1234',} data = {# 'secretstr ': reserve [1], 'train _ date': '1970-01-17 ', 'back _ train_date': '1970-01-17 ', 'tour _ flag': 'dc ', # dc One-Way 'purpose _ Code': 'Adult ', # ADULT Adult pass 'query _ from_station_name ': 'chengdu', 'query _ to_station_name ': 'changsha', 'undefined': ''} def ticket (): # First log in and then query the ticket information signin () url = ('https: // kyf201712306.cn/otn/leftticket/queryz? LeftTicketDTO. train_date = 2012-2-10 & ''ftticketdto. from_station = SZQ & leftTicketDTO. to_station = NFG & purpose_codes = ADULT ') try: response = requests. get (url, verify = False) result = response. json () print (result) return result ['data'] ['result'] cannot Exception as e: return Noneif _ name _ = "_ main _": # ticket () # with open ('. /aaa.xlsx ', encoding = 'utf-8') as f: # print (f. read () dic = {} context = [('IDs _ ABOUT_OFFICAL_PHONE ', 'slu extension beni telefon'), ('IDs _ ABOUT_OFFICAL_WEBSITE ', 'slu evaluate beno web-mjesto '), ('IDs _ ABOUT_OFFICIAL_PHONE_CALL_NOT_SUPPORT', 'trenutni ure has aj ne mo has e pozivati. ')] for I in range (len (context): print (I) dic. setdefault (context [I] [0], context [I] [1]) print (dic. keys ())
Summary
The above is the Python 12306 logon instance Code introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave a message and the editor will reply to you in time. Thank you very much for your support for the help House website!