Python 12306抢火车票脚本
作者:zqw_4181 时间:2023-09-12 13:36:11
本文实例为大家分享了Python 12306抢火车票的具体代码,供大家参考,具体内容如下
# -*- coding: utf-8 -*-
from splinter.browser import Browser
from time import sleep
import traceback
import time, sys
class huoche(object):
"""docstring for huoche"""
driver_name = ''
executable_path = ''
# 用户名,密码
username = u"name"
passwd = u"passwd"
# cookies值得自己去找, 下面两个分别是上海, 太原南
starts = u"%u4E0A%u6D77%2CSHH"
ends = u"%u592A%u539F%2CTYV"
# 时间格式2018-01-19
dtime = u"2018-01-19"
# 车次,选择第几趟,0则从上之下依次点击
order = 0
###乘客名
users = [u"你的名字"]
##席位
xb = u"二等座"
pz = u"成人票"
"""网址"""
ticket_url = "https://kyfw.12306.cn/otn/leftTicket/init"
login_url = "https://kyfw.12306.cn/otn/login/init"
initmy_url = "https://kyfw.12306.cn/otn/index/initMy12306"
buy = "https://kyfw.12306.cn/otn/confirmPassenger/initDc"
def __init__(self):
self.driver_name = 'chrome'
self.executable_path = 'chromedriver.exe'
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)
self.driver.find_by_text(u"登录").click()
u"等待验证码,自行输入..."
print("start to login")
while True:
if self.driver.url != self.initmy_url:
sleep(1)
else:
break
print("end to login")
def start(self):
self.driver = Browser(driver_name=self.driver_name, executable_path=self.executable_path)
self.driver.driver.set_window_size(1400, 1000)
self.login()
# sleep(1)
self.driver.visit(self.ticket_url)
try:
u"购票页面开始..."
# 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.dtime})
self.driver.reload()
count = 0
if self.order != 0:
while self.driver.url == self.ticket_url:
self.driver.find_by_text(u"查询").click()
count += 1
u"循环点击查询... 第 %s 次" % count
# sleep(1)
try:
self.driver.find_by_text(u"预订")[self.order - 1].click()
except Exception as e:
e
u"还没开始预订"
continue
else:
while self.driver.url == self.ticket_url:
self.driver.find_by_text(u"查询").click()
count += 1
u"循环点击查询... 第 %s 次" % count
# sleep(0.8)
try:
for i in self.driver.find_by_text(u"预订"):
i.click()
sleep(1)
except Exception as e:
e
u"还没开始预订 %s" % count
continue
u"开始预订..."
# sleep(3)
# self.driver.reload()
sleep(1)
u'开始选择用户...'
for user in self.users:
self.driver.find_by_text(user).last.click()
u"提交订单..."
sleep(1)
# self.driver.find_by_text(self.pz).click()
# self.driver.find_by_id('').select(self.pz)
# # sleep(1)
# self.driver.find_by_text(self.xb).click()
# sleep(1)
self.driver.find_by_id('submitOrder_id').click()
# print u"开始选座..."
# self.driver.find_by_id('1D').last.click()
# self.driver.find_by_id('1F').last.click()
sleep(1.5)
u"确认选座..."
self.driver.find_by_id('qr_submit_id').click()
except Exception as e:
e
cities = {'成都': '%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'}
if __name__ == '__main__':
huoche = huoche()
huoche.starts = cities["杭州"]
huoche.ends = cities["上海"]
huoche.dtime = "2018-02-05"
huoche.start()
来源:http://blog.csdn.net/zqw_4181/article/details/79153323
标签:Python,抢票脚本
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL自增列插入0值的解决方案
2024-01-22 00:15:09
基于TensorFlow的CNN实现Mnist手写数字识别
2022-03-13 11:20:28
![](https://img.aspxhome.com/file/2023/0/99430_0s.png)
Pycharm中的下载安装、配置与测试方式
2023-11-18 01:53:27
![](https://img.aspxhome.com/file/2023/2/101212_0s.png)
golang gorm的预加载及软删硬删的数据操作示例
2024-04-25 13:18:34
form 元素内的字段 name 不要跟 form 属性名称一致
2008-10-22 13:25:00
简析Python的闭包和装饰器
2021-06-25 03:25:40
![](https://img.aspxhome.com/file/2023/4/82654_0s.png)
分享8点超级有用的Python编程建议(推荐)
2022-03-31 08:05:57
![](https://img.aspxhome.com/file/2023/2/66432_0s.png)
python在windows命令行下输出彩色文字的方法
2021-11-10 09:20:36
详解Django 时间与时区设置问题
2021-02-19 03:08:42
Python入门教程(三十)Python的PIP
2022-03-08 02:32:51
![](https://img.aspxhome.com/file/2023/4/95284_0s.jpg)
pycharm实现猜数游戏
2023-01-14 19:04:49
![](https://img.aspxhome.com/file/2023/5/67385_0s.jpg)
mysql慢查询日志轮转_MySQL慢查询日志实操
2024-01-26 05:54:14
![](https://img.aspxhome.com/file/2023/5/69775_0s.png)
vue2.0+vue-dplayer实现hls播放的示例
2024-05-29 22:46:56
![](https://img.aspxhome.com/file/2023/2/123002_0s.png)
SQL提高查询效率之Like篇
2011-10-01 09:36:42
详谈LABJS按需动态加载js文件
2024-04-23 09:20:56
一篇文章讲解清楚MySQL索引
2024-01-28 11:59:24
![](https://img.aspxhome.com/file/2023/3/69253_0s.png)
Python设计模式之抽象工厂模式原理与用法详解
2023-01-25 16:05:43
![](https://img.aspxhome.com/file/2023/0/76130_0s.png)
numpy自动生成数组详解
2023-09-02 22:53:47
Python for 循环语句的使用
2023-10-29 14:51:12
mysql定时自动备份数据库的方法步骤
2024-01-15 10:01:19
![](https://img.aspxhome.com/file/2023/5/116745_0s.png)