#!/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':'',
'00:00--06:00':'',
'06:00--12:00':'',
'12:00--18:00':'',
'18:00--24:00':'',
} # 城市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(如果报错请查看包是否引用正确或官网是否改动)

python写12306抢票的更多相关文章

  1. Python操作12306抢票脚本

    有一段时间没有使用Python了,前几天经朋友提起一篇关于用Python实现抢火车票的文章,百度了实现抢火车票的技术细节,网上却有不少资料,也不是新鲜的东西.在了解了一些技术手段,阅读了一些大神的博文 ...

  2. Python 实现的 12306抢票脚本

    Python12306抢票脚本 本脚本使用一个类来实现所有代码,大体上分为以下几个模块及其步骤:- 初始化对象属性(在抢票前进行的属性初始化,包括初始化浏览器模拟对象,个人信息等).- 建立模拟浏览器 ...

  3. 四、基于HTTPS协议的12306抢票软件设计与实现--水平DNS并发查询分享

    一.基于HTTPS协议的12306抢票软件设计与实现--实现效果 二.基于HTTPS协议的12306抢票软件设计与实现--相关接口以及数据格式 三.基于HTTPS协议的12306抢票软件设计与实现-- ...

  4. 12306抢票带来的启示:看我如何用Go实现百万QPS的秒杀系统

    本文为开源实验性工程:“github.com/GuoZhaoran/spikeSystem”的配套文章,原作者:“绘你一世倾城”,现为:猎豹移动php开发工程师,感谢原作者的技术分享. 1.引言 Go ...

  5. 12306抢票算法居然被曝光了!!!居然是redis实现的

    导读 相信大家应该都有抢火车票的经验,每年年底,这都是一场盛宴.然而你有没有想过抢火车票这个算法是怎么实现的呢? 应该没有吧,咱们今天就来一一探讨.其实并没有你想的那么难 bitmap与位运算 red ...

  6. 「今日 GitHub 趋势」让全世界程序员体会中国的 12306 抢票狂潮

    [2018年1月7日 GitHub 趋势] No.1:yyx990803 / build-your-own-mint 单日 714 星 使用 Plaid,Google 表格和 CircleCI 构建您 ...

  7. 12306 抢票项目霸榜 GitHub,标星即将破万

    十一将至,你买到回家的火车票了吗?如果没有,你可以试着打开 GitHub,在搜索栏键入 12306 的关键词,我相信你会发现一个新大陆.没错,这里有 1572 个抢票项目.它们大多用 Python.J ...

  8. python3.7之12306抢票脚本实现

    悲催的12306,彻底沦为各路抢票软件的服务提供方.元旦伊始,纯粹12306官网及APP抢票,愈一周的时间,仅到手一张凌晨3:55回家的站票.为远离脑残,无奈选择抢票软件,预购年后返沪车票.BTW,研 ...

  9. 马后炮之12306抢票工具(四)--抢票Demo,2014年1月9日终结版

    时隔一年多,终于朋友的忽悠下吧抢票Demo的最后一步完善了,与2014年1月9日成功生成车票. Demo仅经过自己测试,并未在高峰期进行测试,代码质量很差,因为赶工,套用去年模板并未使用设计模式. 代 ...

随机推荐

  1. k8s学习-资源清单

    4.kubernetes使用 4.1.资源清单 api 文档.api 描述 4.1.2.说明 必须存在的属性 主要的对象 额外的参数项 例子 vim my-app.yml apiVersion: v1 ...

  2. Spring Boot 教程 - Elasticsearch

    1. Elasticsearch简介 Elasticsearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearc ...

  3. lei muban

    #include<iostream> using namespace std; template <typename T> class Operator{ public: T ...

  4. 如何在react中使用decorator

    2020-03-27 如何在react中使用decorator decorator目前都需要修改babel才能使用 说一下具体的操作方法 踩了一天的坑... 步骤1: yarn create reac ...

  5. 多线程实现tcp聊天服务器

    多线程tcp  server & client tcp服务端(多线程): from socket import * from threading import Thread def clien ...

  6. TensorFlow从0到1之TensorFlow csv文件读取数据(14)

    大多数人了解 Pandas 及其在处理大数据文件方面的实用性.TensorFlow 提供了读取这种文件的方法. 前面章节中,介绍了如何在 TensorFlow 中读取文件,本节将重点介绍如何从 CSV ...

  7. 【JMeter_04】JMeter 插件管理、语言设置

    语言设置 JMeter是外来午中,初始默认语言为英文,如果有朋友更倾向于使用中文或者其他语言,那么可以通过以下两种方法来切换,随着JMeter版本的不断升级,会发现程序的汉化支持已经越来越完善了. 1 ...

  8. 1、python简介-变量-注释-数据类型-用户交互-if语句-while循坏

    一. 计算机是什么 基本组成: 主板+cpu+内存 cpu: 主频, 核数(16) 内存:大小(8G, 16G, 32G) 型号: DDR3, DDR4, DDR5, 主频(海盗船,玩家国度) 显卡: ...

  9. Redis源码阅读一:简单动态字符串SDS

    源码阅读基于Redis4.0.9 SDS介绍 redis 127.0.0.1:6379> SET dbname redis OK redis 127.0.0.1:6379> GET dbn ...

  10. MOJITO 发布一周,爬一波弹幕分析下

    MOJITO 最近一直啥都没写,追个热点都赶不上热乎的,鄙视自己一下. 周董的新歌 「MOJITO」 发售(6 月 12 日的零点)至今大致过去了一周,翻开 B 站 MV 一看,播放量妥妥破千万,弹幕 ...