您好,登錄后才能下訂單哦!
小編給大家分享一下python如何實(shí)現(xiàn)自動(dòng)12306搶票軟件,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
示例:
import requests import time import dmpt import re import random from copyheaders import headers_raw_to_dict DEFAULT_HEADERS={ '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/537.36', 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'Referer':'https://kyfw.12306.cn/otn/index/init', 'Accept-Language':'zh-CN,zh;q=0.9', } def get_random(): return str(random.random()) #生產(chǎn)一個(gè)18位的隨機(jī)數(shù) def get_13_time(): #一個(gè)十三位的時(shí)間戳 return str(int(time.time()*1000)) class CN12306(object): def __init__(self): self.chufa='2018-02-03' self.s=requests.session() self.s.verify = False # 忽略https 證書驗(yàn)證 def get_init(self): #請求了一個(gè)首頁 url='https://kyfw.12306.cn/otn/login/init' r=self.s.get(url) print('首頁獲取成功,狀態(tài)碼:',r) def get_newpasscode(self): #這個(gè)頁面不知道是干啥的,但是12306 請求了,咱們?yōu)榱四7碌南褚稽c(diǎn)也去請求 url='https://kyfw.12306.cn/otn/resources/js/newpasscode/captcha_js.js?_={}'.format(get_13_time()) r=self.s.get(url) print('newpasscode獲取成功,狀態(tài)碼:',r) def get_auth_code(self): #獲取驗(yàn)證碼 url='https://kyfw.12306.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): # 調(diào)用打碼平臺,返回驗(yàn)證碼坐標(biāo) dmt = dmpt.DamatuApi("打碼平臺帳號", "打碼平臺密碼") analysis_auth = dmt.decode('auth_code.png', 287) # 上傳打碼 analysis_auth = re.sub('\|', ',', analysis_auth) li = 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('驗(yàn)證碼坐標(biāo)', analysis_auth) self.analysis_auth= analysis_auth # 驗(yàn)證碼坐標(biāo) def auth_auth_code(self): #驗(yàn)證驗(yàn)證碼是否正確提交方式post url='https://kyfw.12306.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":"驗(yàn)證碼校驗(yàn)成功","result_code":"4"}''': #驗(yàn)證碼校驗(yàn)成功 return True else: #如果驗(yàn)證碼校驗(yàn)失敗 print('驗(yàn)證碼錯(cuò)誤,刷新驗(yàn)證碼,重新提交') if self.get_auth_code(): #獲取驗(yàn)證碼 self.analysis_auth_code() #調(diào)用打碼平臺 self.auth_auth_code() #重新校驗(yàn)驗(yàn)證碼 def login(self): url='https://kyfw.12306.cn/passport/web/login' data={ 'username' : '12306帳號', '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://kyfw.12306.cn/otn/login/userLogin' r=self.s.post(url=url) # print(r.text) def getjs(self): #不知道是干啥的,但是也提交吧 url='https://kyfw.12306.cn/otn/HttpZF/GetJS' r=self.s.get(url) def post_uamtk(self): url='https://kyfw.12306.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://kyfw.12306.cn/otn/uamauthclient' data={ 'tk':self.newapptk } r=self.s.post(url=url,data=data) self.apptk = r.json()["apptk"] r.encoding='utf-8' print(r.text) def get_userLogin(self): url='https://kyfw.12306.cn/otn/login/userLogin' r=self.s.get(url) r.encoding='utf-8' # print(r.text) def get_leftTicket(self): url='https://kyfw.12306.cn/otn/leftTicket/init' r=self.s.get(url) r.encoding='utf-8' # print(r.text) def get_GetJS(self): url='https://kyfw.12306.cn/otn/HttpZF/GetJS' self.s.get(url) def get_qufzjql(self): url = 'https://kyfw.12306.cn/otn/dynamicJs/qufzjql' self.s.get(url) def get_queryZ(self): url='https://kyfw.12306.cn/otn/leftTicket/queryZ?leftTicketDTO.train_date={}&leftTicketDTO.from_station={}&leftTicketDTO.to_station={}&purpose_codes={}'.format(self.chufa,'BJP','TBP','ADULT') 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]) 車次=str(dandulist[3]) 出發(fā)時(shí)間=str(dandulist[8]) 到達(dá)時(shí)間 = str(dandulist[9]) 歷時(shí)=str(dandulist[10]) 軟臥 = str(dandulist[23]) 硬臥=str(dandulist[28]) print(i) print('可預(yù)訂車次列表,','車次:',車次,'出發(fā)時(shí)間:', 出發(fā)時(shí)間,'到達(dá)時(shí)間:', 到達(dá)時(shí)間,'歷時(shí):', 歷時(shí),'軟臥剩余: ',軟臥,' 硬臥剩余: ',硬臥) if (軟臥 != '' and 軟臥 != '0' and 軟臥 != '無' and 軟臥 != '空') or (硬臥 != '' and 硬臥 != '0' and 硬臥 != '無' and 硬臥 != '空'): #執(zhí)行下單操作 self.post_submitOrderRequest() self.post_initDc() self.post_getPassengerDTOs() return False print('*****************************************************') return True # 點(diǎn)擊預(yù)定下單 def post_submitOrderRequest(self): url='https://kyfw.12306.cn/otn/leftTicket/submitOrderRequest' data={ 'secretStr':self.secretStr, 'train_date':self.chufa, #出發(fā)時(shí)間 'back_train_date':self.chufa ,#返回時(shí)間 'tour_flag':'dc', 'purpose_codes':'ADULT', 'query_from_station_name':'北京', 'query_to_station_name':'天津北', 'undefined':'' } r=self.s.post(url=url,data=data) print(r.text) def post_initDc(self): url='https://kyfw.12306.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): #獲取乘客信息 url='https://kyfw.12306.cn/otn/confirmPassenger/getPassengerDTOs' 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(): #如果驗(yàn)證碼獲取成功,就調(diào)用打碼平臺 print('驗(yàn)證碼獲取成功') print('正在調(diào)用打碼平臺...') cn.analysis_auth_code() if cn.auth_auth_code(): #驗(yàn)證驗(yàn)證碼是否正確 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)
看完了這篇文章,相信你對“python如何實(shí)現(xiàn)自動(dòng)12306搶票軟件”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。