#!/usr/bin/env python # -*- coding: utf-8 -*- ''' 利用splinter寫的一個手動過驗證及自動搶票的例子, 大家可以自己擴展或者弄錯窗體、web端。 本例子只做參考。 本代碼發布於2018.12.18(如果報錯請查看官網是否改動) ''' import re from splinter.browser import Browser from time import sleep import sys import httplib2 from urllib import parse import smtplib from email.mime.text import MIMEText class BrushTicket(object): """買票類及實現方法""" def __init__(self,train_date, user_name, password, passengers, from_time, from_station, to_station, seat_type, receiver_mobile, receiver_email,isHave=False,): """定義實例屬性,初始化""" # 有票就行 self.isHave = isHave # 1206賬號密碼 self.user_name = user_name self.password = password # 乘客姓名 self.passengers = passengers # 起始站和終點站 self.from_station = from_station self.to_station = to_station # 乘車日期 self.from_time = from_time #發車時間 self.train_date=train_date # 座位類型所在td位置 if seat_type == '商務座特等座': seat_type_index = 1 seat_type_value = 9 elif seat_type == '一等座': seat_type_index = 2 seat_type_value = 'M' elif seat_type == '二等座': seat_type_index = 3 seat_type_value = 0 elif seat_type == '高級軟卧': seat_type_index = 4 seat_type_value = 6 elif seat_type == '軟卧': seat_type_index = 5 seat_type_value = 4 elif seat_type == '動卧': seat_type_index = 6 seat_type_value = 'F' elif seat_type == '硬卧': seat_type_index = 7 seat_type_value = 3 elif seat_type == '軟座': seat_type_index = 8 seat_type_value = 2 elif seat_type == '硬座': seat_type_index = 9 seat_type_value = 1 elif seat_type == '無座': seat_type_index = 10 seat_type_value = 1 elif seat_type == '其他': seat_type_index = 11 seat_type_value = 1 else: seat_type_index = 7 seat_type_value = 3 self.seat_type_index = seat_type_index self.seat_type_value = seat_type_value # 通知信息 self.receiver_mobile = receiver_mobile self.receiver_email = receiver_email # 主要頁面網址 self.login_url = 'https://kyfw.12306.cn/otn/login/init' self.init_my_url = 'https://kyfw.12306.cn/otn/view/index.html' self.ticket_url = 'https://kyfw.12306.cn/otn/leftTicket/init' # 瀏覽器驅動信息,驅動下載頁:https://sites.google.com/a/chromium.org/chromedriver/downloads self.driver_name = 'chrome' #驅動的位置 self.executable_path = 'C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe' def do_login(self): """登錄功能實現,手動識別驗證碼進行登錄""" self.driver.visit(self.login_url) sleep(1) self.driver.fill('loginUserDTO.user_name', self.user_name) self.driver.fill('userDTO.password', self.password) print('請輸入驗證碼……') while True: if self.driver.url != self.init_my_url: sleep(1) else: break def start_brush(self): """買票功能實現""" self.driver = Browser(driver_name=self.driver_name,executable_path=self.executable_path) # 瀏覽器窗口的大小 # self.driver.driver.set_window_size(900, 700) self.do_login() self.driver.visit(self.ticket_url) try: print('開始刷票……') # 加載車票查詢信息 self.driver.cookies.add({"_jc_save_fromStation": self.from_station})#出發位置 self.driver.cookies.add({"_jc_save_toStation": self.to_station})#目的地 self.driver.cookies.add({"_jc_save_fromDate": self.from_time})#出發時間 self.driver.reload() count = 0 while self.driver.url.split('?')[0] == self.ticket_url: try: self.wait_time('query_ticket') elemt= self.driver.find_by_xpath('//select[@id="cc_start_time"]//option[@value="'+str(self.train_date)+'"]',).first elemt.click() sleep(1) self.driver.find_by_text('查詢').click() self.wait_time('train_type_btn_all') count += 1 print('第%d次點擊查詢……' % count) elems = self.driver.find_by_id('queryLeftTable')[0].find_by_xpath('//tr[starts-with(@id,"ticket_")]') while len(elems)==0: sleep(0.5) elems = self.driver.find_by_id('queryLeftTable')[0].find_by_xpath('//tr[starts-with(@id,"ticket_")]') #是不是有票就行 if(self.isHave): for current_tr in elems: if(current_tr.text==''): print('沒票') continue # 下標索引 if current_tr.find_by_tag('td')[self.seat_type_index].text == '--' or current_tr.find_by_tag('td')[self.seat_type_index].text == '無': print('無此座位類型出售!') continue else: # 有票,嘗試預訂 print('刷到票了(余票數:' + str(current_tr.find_by_tag('td')[self.seat_type_index].text) + '),開始嘗試預訂……') current_tr.find_by_css('td.no-br>a')[0].click() key_value = 1 # 等待頁面加載完畢 self.wait_time('normalPassenger_' +str(int(key_value-1))) for p in self.passengers: # 選擇用戶 print('開始選擇用戶……') self.driver.find_by_text(p).last.click() # 選擇座位類型 print('開始選擇席別……') if self.seat_type_value != 0: sleep(1) seat_select = self.driver.find_by_id("seatType_" + str(key_value))[0] seat_select.find_by_xpath("//option[@value='" + str(self.seat_type_value) + "']")[0].click() key_value += 1 if p[-1] == ')': self.driver.find_by_id('dialog_xsertcj_ok').click() print('正在提交訂單……') self.driver.find_by_id('submitOrder_id').click() self.wait_time('content_checkticketinfo_id') # 查看返回結果是否正常 submit_false_info = self.driver.find_by_id('orderResultInfo_id')[0].text if submit_false_info != '': print(submit_false_info) self.driver.find_by_id('qr_closeTranforDialog_id').click() self.driver.find_by_id('preStep_id').click() continue print('正在確認訂單……') # 等待加載完畢 self.wait_time('qr_submit_id') self.driver.find_by_id('qr_submit_id').click() print('預訂成功,請及時前往支付……') # 發送通知信息 self.send_mail(self.receiver_email, '恭喜您,搶到票了,請及時前往12306支付訂單!') self.send_sms(self.receiver_mobile, '恭喜您,搶到票了,請及時前往12306支付訂單!') else: for current_tr in elems: if(current_tr.text==''): print('沒票') continue else: # 下標索引 print('判斷車票是否存在') if current_tr.find_by_tag('td')[self.seat_type_index].text == '--' or current_tr.find_by_tag('td')[self.seat_type_index].text == '無': print('無此座位類型出售!') continue else: # 有票,嘗試預訂 print('刷到票了(余票數:' + str(current_tr.find_by_tag('td')[self.seat_type_index].text) + '),開始嘗試預訂……') current_tr.find_by_css('td.no-br>a')[0].click() key_value = 1 # 等待頁面加載完畢 self.wait_time('normalPassenger_' +str(int(key_value-1))) for p in self.passengers: # 選擇用戶 print('開始選擇用戶……') self.driver.find_by_text(p).last.click() # 選擇座位類型 print('開始選擇席別……') if self.seat_type_value != 0: seat_select = self.driver.find_by_id("seatType_" + str(key_value))[0] seat_select.find_by_xpath("//option[@value='" + str(self.seat_type_value) + "']")[0].click() key_value += 1 if p[-1] == ')': self.driver.find_by_id('dialog_xsertcj_ok').click() print('正在提交訂單……') self.driver.find_by_id('submitOrder_id').click() self.wait_time('content_checkticketinfo_id') # 查看返回結果是否正常 submit_false_info = self.driver.find_by_id('orderResultInfo_id')[0].text if submit_false_info != '': print(submit_false_info) self.driver.find_by_id('qr_closeTranforDialog_id').click() self.driver.find_by_id('preStep_id').click() continue print('正在確認訂單……') # 等待加載完畢 self.wait_time('qr_submit_id') self.driver.find_by_id('qr_submit_id').click() print('預訂成功,請及時前往支付……') # 發送通知信息 self.send_mail(self.receiver_email, '恭喜您,搶到票了,請及時前往12306支付訂單!') self.send_sms(self.receiver_mobile, '恭喜您,搶到票了,請及時前往12306支付訂單!') # self.driver.quit() return except Exception as error_info: print(error_info) except Exception as error_info: print(error_info) def wait_time(self, name): while self.driver.is_element_present_by_id(name) == False: sleep(1) def send_sms(self, mobile, sms_info): """發送手機通知短信,用的是-互億無線-的測試短信""" host = "106.ihuyi.com" sms_send_uri = "/webservice/sms.php?method=Submit" account = "C59782899" pass_word = "19d4d9c0796532c7328e8b82e2812655" params = parse.urlencode( {'account': account, 'password': pass_word, 'content': sms_info, 'mobile': mobile, 'format': 'json'} ) headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} conn = httplib2.HTTPConnectionWithTimeout(host, port=80, timeout=30) conn.request("POST", sms_send_uri, params, headers) response = conn.getresponse() response_str = response.read() conn.close() return response_str def send_mail(self, receiver_address, content): """發送郵件通知""" # 連接郵箱服務器信息 host = 'smtp.qq.com' #QQ sender = '673615750@qq.com' # 你的發件郵箱號碼 pwd = '授權碼' # 不是登陸密碼,是客戶端授權密碼 # 發件信息 receiver = receiver_address body = '<h2>溫馨提醒:</h2><p>' + content + '</p>' msg = MIMEText(body, 'html', _charset="utf-8") msg['subject'] = '搶票成功通知!' msg['from'] = sender msg['to'] = receiver s = smtplib.SMTP(host, port=465, timeout=30) # 開始登陸郵箱,並發送郵件 s.login(sender, pwd) s.sendmail(sender, receiver, msg.as_string()) s.close() if __name__ == '__main__': # 12306用戶名 user_name = 'XXXXX' # 12306登陸密碼 password = 'XXXXX' # 乘客姓名 passengers = '張三' # 乘車日期 from_time = '2018-12-21' #發車時間 train_date={ '00:00--24:00':'00002400', '00:00--06:00':'00000600', '06:00--12:00':'06001200', '12:00--18:00':'12001800', '18:00--24:00':'18002400', } # 城市cookie字典 city_list = {'成都': '%u6210%u90FD%2CCDW', '重慶': '%u91CD%u5E86%2CCQW', '北京': '%u5317%u4EAC%2CBJP', '廣州': '%u5E7F%u5DDE%2CGZQ', '杭州': '%u676D%u5DDE%2CHZH', '宜昌': '%u5B9C%u660C%2CYCN', '鄭州': '%u90D1%u5DDE%2CZZF', '深圳': '%u6DF1%u5733%2CSZQ', '西安': '%u897F%u5B89%2CXAY', '大連': '%u5927%u8FDE%2CDLT', '武漢': '%u6B66%u6C49%2CWHN', '上海': '%u4E0A%u6D77%2CSHH', '南京': '%u5357%u4EAC%2CNJH', '合肥': '%u5408%u80A5%2CHFH'} # 出發站 from_station = city_list['廣州'] # 終點站 to_station = city_list['武漢'] # 座位類型 seat_type = '二等座' # 搶票成功,通知該手機號碼 receiver_mobile = 'xxxxxx' #搶票成功,通知該郵件 receiver_email = 'xxx@qq.com' # 開始搶票 ticket = BrushTicket(train_date['18:00--24:00'],user_name, password, passengers.split(","), from_time, from_station, to_station, seat_type, receiver_mobile, receiver_email,True) ticket.start_brush()
環境:python3,chromedriver(請下載對應的版本的瀏覽器驅動)
效果圖:


本代碼發布於2018.12.18(如果報錯請查看包是否引用正確或官網是否改動)
