国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python+Splinter實現12306搶票功能

2020-01-04 14:28:06
字體:
來源:轉載
供稿:網友

本文實例為大家分享了python實現12306搶票功能的具體代碼,供大家參考,具體內容如下

源碼記錄如下:

#!/usr/bin/env python# _*_ coding:utf-8 _*_#!/usr/bin/env python# _*_ coding:utf-8 _*_from splinter.browser import Browserfrom time import sleepimport os# from selenium.webdriver.chrome.options import Optionsimport loggingfrom log_class import Logger # 需要一個logger庫import sysreload(sys)sys.setdefaultencoding('utf-8') # 防止由于Unicode編碼與ASCII編碼的不兼容造成錯誤class BuyTicket(object):  def __init__(self, username, passwd, order, passengers, seatType, ticketType, daytime, starts, ends):    # 用戶名 密碼    self.username = username    self.passwd = passwd    # 車次,選擇第幾趟,0則從上之下依次點擊    self.order = order    # 乘客名    self.passengers = passengers    # 席位    self.seatType = seatType    self.ticketType = ticketType    # 時間格式2018-02-05    self.daytime = daytime    # 起始地和終點    self.starts = starts    self.ends = ends    self.login_url = 'https://kyfw.12306.cn/otn/login/init'    self.initMy_url = 'https://kyfw.12306.cn/otn/index/initMy12306'    self.ticket_url = 'https://kyfw.12306.cn/otn/leftTicket/init'    # 瀏覽器名稱    self.driver_name = 'firefox' # chrome firefox    # 火狐瀏覽器第三方驅動    self.executable_path = os.getcwd()+'/geckodriver' # 獲取工程目錄下的火狐驅動 chromedriver  def login(self):    # 訪問登錄網址    self.driver.visit(self.login_url)    # 填充用戶名    self.driver.fill("loginUserDTO.user_name", self.username)    # sleep(1)    # 填充密碼    self.driver.fill("userDTO.password", self.passwd)    logbticket.info("請手動輸入驗證碼...")    # print('請手動輸入驗證碼...') # 目前沒有自動驗證碼    # 循環等待登錄,登錄成功,跳出循環    while True:      if self.driver.url != self.initMy_url:        sleep(1)      else:        break  def start_buy(self):    # 這些設置都是必要的    # chrome_options = Options()    # chrome_options.add_argument("--no-sandbox")    # chrome_options.add_argument("--disable-setuid-sandbox")    # chrome_options.add_argument("disable-infobars") # 禁用網頁上部的提示欄    # self.driver = Browser(driver_name=self.driver_name, options=chrome_options, executable_path=self.executable_path)    self.driver = Browser(driver_name=self.driver_name,               executable_path=self.executable_path)    # 設置窗口大小尺寸    self.driver.driver.set_window_size(1400, 1000)    # 用戶登錄    self.login()    # 進入選票網站    self.driver.visit(self.ticket_url)    try:      logbticket.info("購票頁面開始....")      # print("購票頁面開始....")      # sleep(1)      # 加載查詢信息      self.driver.cookies.add({"_jc_save_fromStation": self.starts})      self.driver.cookies.add({"_jc_save_toStation": self.ends})      self.driver.cookies.add({"_jc_save_fromDate": self.daytime})      self.driver.reload()      count = 0      if self.order != 0:        while self.driver.url == self.ticket_url:          self.driver.find_by_text("查詢").click()          count = count+1          logbticket.info("第 %d 次點擊查詢..." % count)          # print("第 %d 次點擊查詢..." % count)          # sleep(1)          try:            self.driver.find_by_text("預訂")[self.order - 1].click() # 點擊第幾個“預訂”            sleep(1.5)          except Exception as e: # e是Exception 的一個instance            # print(e)            # print("預訂失敗...")            logbticket.error(e)            logbticket.error("預訂失敗...")            continue      else:        while self.driver.url == self.ticket_url:          self.driver.find_by_text("查詢").click()          count += 1          logbticket.info("第 %d 次點擊查詢..." % count)          # print("第 %d 次點擊查詢..." % count)          try:            for i in self.driver.find_by_text("預訂"):              i.click()              sleep(1)          except Exception as e:            # print(e)            # print("預訂失敗...")            logbticket.error(e)            logbticket.error("預訂失敗...")            continue      # print("開始預訂....")      logbticket.info("開始預訂....")      # sleep(1)      # self.driver.reload()      sleep(1)      # print("開始選擇用戶....")      logbticket.info("開始選擇用戶....")      for p in self.passengers:        pg = self.driver.find_by_text(p) # .last.click()        pg.last.click()      # print("提交訂單....")      logbticket.info("提交訂單....")      sleep(1)      i = 0      while len(self.passengers) > 0:        i = i + 1        seat_id_string = "seatType_" + str(i)        ticket_id_string = "ticketType_" + str(i)        self.driver.find_by_xpath('//select[@id="%s"]/option[@value="%s"]'                     % (seat_id_string, self.seatType)).first._element.click()        self.driver.find_by_xpath('//select[@id="%s"]//option[@value="%s"]'                     % (ticket_id_string, self.ticketType)).first._element.click()        # self.driver.select("confirmTicketType", "3")        self.passengers.pop()        sleep(1)      self.driver.find_by_id("submitOrder_id").click()      # print("開始選座...")      logbticket.info("開始選座...")      sleep(1.5)      # print("確認選座....")      logbticket.info("確認選座....")      self.driver.find_by_text("qr_submit_id").click()    except Exception as e:      # print(e)      logbticket.error(e)city = {"深圳": "%u6DF1%u5733%2CSZQ",    "武漢": "%u6B66%u6C49%2CWHN",    "隨州": "%u968F%u5DDE%2CSZN"}seatT = {"硬臥": "3",     "軟臥": "4",     "硬座": "1",     "二等座": "O",     "一等座": "M",     "商務座": "9"}if __name__ == '__main__':  # 用戶名  username = "xxxxxxxx"  # 密碼  password = "xxxxxx"  # 車次選擇,0代表所有車次  order = 13  # 乘客名,比如passengers = ['丁小紅', '丁小明']  passengers = ["xxx", "xxx"]  # 日期,格式為:'2018-01-20'  daytime = "2018-04-05"  # 出發地(需填寫cookie值)  starts = city["xx"] # 武漢  # 目的地(需填寫cookie值)  ends = city["xx"] # 北京  # 席別  seatType = seatT["二等座"] # 二等座  # 票種  ticketType = "1" # 成人票  logbticket = Logger("bticket.log", logging.DEBUG, logging.ERROR)  BuyTicket(username, password, order, passengers, seatType, ticketType, daytime, starts, ends).start_buy()

火狐瀏覽器的驅動下載地址

logger庫文件:

源代碼如下:

#!/usr/bin/env python# _*_ coding:utf-8 _*_import loggingclass Logger:  def __init__(self, path, clevel=logging.DEBUG, Flevel=logging.DEBUG):    self.logger = logging.getLogger(path)    self.logger.setLevel(logging.DEBUG)    fmt = logging.Formatter('[%(asctime)s] [%(levelname)s] %(message)s', '%Y-%m-%d %H:%M:%S')    # 設置CMD日志    sh = logging.StreamHandler()    sh.setFormatter(fmt)    sh.setLevel(clevel)    # 設置文件日志    fh = logging.FileHandler(path)    fh.setFormatter(fmt)    fh.setLevel(Flevel)    self.logger.addHandler(sh)    self.logger.addHandler(fh)  def debug(self, message):    self.logger.debug(message)  def info(self, message):    self.logger.info(message)  def war(self, message):    self.logger.warn(message)  def error(self, message):    self.logger.error(message)  def cri(self, message):    self.logger.critical(message)if __name__ == '__main__':  logyyx = Logger('yyx.log', logging.ERROR, logging.DEBUG)  logyyx.debug('一個debug信息')  logyyx.info('一個info信息')  logyyx.war('一個warning信息')  logyyx.error('一個error信息')  logyyx.cri('一個致命critical信息')

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 景宁| 广宗县| 西青区| 岫岩| 枞阳县| 定远县| 若尔盖县| 长寿区| 乐安县| 城口县| 吉林省| 揭东县| 临泉县| 道真| 中超| 清水河县| 黑河市| 礼泉县| 神农架林区| 武乡县| 龙口市| 石阡县| 依安县| 绍兴县| 龙胜| 翁牛特旗| 集贤县| 南丰县| 涟源市| 云龙县| 喀什市| 阿鲁科尔沁旗| 洛川县| 秭归县| 东阿县| 达尔| 平陆县| 襄樊市| 织金县| 镇坪县| 水富县|