亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python自動(dòng)12306搶票軟件實(shí)現(xiàn)代碼

 更新時(shí)間:2018年02月24日 09:55:14   作者:趙韶暉_  
這篇文章主要為大家詳細(xì)介紹了python自動(dòng)12306搶票軟件的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

昨天我發(fā)的是抓取的12306數(shù)據(jù)包,然后分析了一下,今天按照昨天的分析 用代碼實(shí)現(xiàn)了,如果有需要的同學(xué)們可以看一下,實(shí)現(xiàn)的功能有,登錄,驗(yàn)證碼識(shí)別,自動(dòng)查票,有余票點(diǎn)擊預(yù)定, 差了最后一步提交訂單。同學(xué)們可以自己研究一下。

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 證書(shū)驗(yàn)證
  def get_init(self): #請(qǐng)求了一個(gè)首頁(yè)
    url='https://kyfw.12306.cn/otn/login/init'
    r=self.s.get(url)
    print('首頁(yè)獲取成功,狀態(tài)碼:',r)

  def get_newpasscode(self): #這個(gè)頁(yè)面不知道是干啥的,但是12306 請(qǐng)求了,咱們?yōu)榱四7碌南褚稽c(diǎn)也去請(qǐng)求
    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)用打碼平臺(tái),返回驗(yàn)證碼坐標(biāo)
    dmt = dmpt.DamatuApi("打碼平臺(tái)帳號(hào)", "打碼平臺(tái)密碼")
    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)用打碼平臺(tái)
        self.auth_auth_code() #重新校驗(yàn)驗(yàn)證碼

  def login(self):
    url='https://kyfw.12306.cn/passport/web/login'
    data={
    'username' : '12306帳號(hào)',
    '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])
        車(chē)次=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ù)訂車(chē)次列表,','車(chē)次:',車(chē)次,'出發(fā)時(shí)間:', 出發(fā)時(shí)間,'到達(dá)時(shí)間:', 到達(dá)時(shí)間,'歷時(shí):', 歷時(shí),'軟臥剩余: ',軟臥,' 硬臥剩余: ',硬臥)
        if (軟臥 != '' and 軟臥 != '0' and 軟臥 != '無(wú)' and 軟臥 != '空') or (硬臥 != '' and 硬臥 != '0' and 硬臥 != '無(wú)' 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)用打碼平臺(tái)
    print('驗(yàn)證碼獲取成功')
    print('正在調(diào)用打碼平臺(tái)...')
    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)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python將原圖裁剪為固定尺寸小圖

    python將原圖裁剪為固定尺寸小圖

    這篇文章主要為大家詳細(xì)介紹了python將原圖裁剪為固定尺寸小圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • Python?matplotlib?繪制散點(diǎn)圖詳解建議收藏

    Python?matplotlib?繪制散點(diǎn)圖詳解建議收藏

    在數(shù)據(jù)統(tǒng)計(jì)圖表中,有一種圖表是散列點(diǎn)分布在坐標(biāo)中,反應(yīng)數(shù)據(jù)隨著自變量變化的趨勢(shì)。這篇文章主要介紹了如何通過(guò)matplotlib繪制散點(diǎn)圖,需要的朋友可以參考一下
    2021-12-12
  • Python中實(shí)現(xiàn)單例模式的n種方式和原理

    Python中實(shí)現(xiàn)單例模式的n種方式和原理

    這篇文章主要介紹了Python中實(shí)現(xiàn)單例模式的n種方式和原理,需要的朋友可以參考下
    2018-11-11
  • 淺談Python2、Python3相對(duì)路徑、絕對(duì)路徑導(dǎo)入方法

    淺談Python2、Python3相對(duì)路徑、絕對(duì)路徑導(dǎo)入方法

    今天小編就為大家分享一篇淺談Python2、Python3相對(duì)路徑、絕對(duì)路徑導(dǎo)入方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • python標(biāo)準(zhǔn)庫(kù) datetime的astimezone設(shè)置時(shí)區(qū)遇到的坑及解決

    python標(biāo)準(zhǔn)庫(kù) datetime的astimezone設(shè)置時(shí)區(qū)遇到的坑及解決

    這篇文章主要介紹了python標(biāo)準(zhǔn)庫(kù) datetime的astimezone設(shè)置時(shí)區(qū)遇到的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Python Selenium中常用的元素定位方法總結(jié)

    Python Selenium中常用的元素定位方法總結(jié)

    在Web自動(dòng)化測(cè)試中,元素定位是一項(xiàng)非常重要的技術(shù),Python Selenium提供了各種元素定位方法,可以幫助我們定位頁(yè)面上的元素并與之交互,本文將詳細(xì)介紹Python Selenium中常用的元素定位方法,并提供實(shí)例代碼,需要的朋友可以參考下
    2023-11-11
  • python中map的基本用法示例

    python中map的基本用法示例

    map函數(shù)的原型是map(function, iterable, …),它的返回結(jié)果是一個(gè)列表。下面這篇文章主要給大家介紹了關(guān)于python中map的基本用法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-09-09
  • 淺析對(duì)torch.unsqueeze()函數(shù)理解

    淺析對(duì)torch.unsqueeze()函數(shù)理解

    torch.unsqueeze()函數(shù)起到升維的作用,dim等于幾表示在第幾維度加一,這篇文章主要介紹了對(duì)torch.unsqueeze()函數(shù)理解深度解析,感興趣的朋友跟隨小編一起看看吧
    2024-06-06
  • Python爬取動(dòng)態(tài)網(wǎng)頁(yè)中圖片的完整實(shí)例

    Python爬取動(dòng)態(tài)網(wǎng)頁(yè)中圖片的完整實(shí)例

    這篇文章主要給大家介紹了關(guān)于Python爬取動(dòng)態(tài)網(wǎng)頁(yè)中圖片的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • python動(dòng)態(tài)加載變量示例分享

    python動(dòng)態(tài)加載變量示例分享

    這篇文章主要介紹了python動(dòng)態(tài)加載變量示例,需要的朋友可以參考下
    2014-02-02

最新評(píng)論