#!/usr/bin/env python

#-*- coding: utf-8 -*-

"""

火车票

可以自动填充账号密码,同时,在登录时,也可以修改账号密码

然后手动识别验证码,并登陆,接下来的事情,交由脚本来做了,静静的等待抢票结果就好(刷票过程中,浏览器不可关闭)

"""

import re

from splinter.browser import Browser

from time import sleep

import time

import sys

import httplib2

from urllib import parse

import smtplib

import numpy as np

from email.mime.text import MIMEText

class BrushTicket(object):

"""买票类及实现方法"""

def __init__(self, user_name, password, passengers, from_time, from_station, to_station, number, seat_type):

"""定义实例属性,初始化"""

# 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.number = number.capitalize()

# 座位类型所在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.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 = r'H:\For personal use\PYTHON\file\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(1000, 900)

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:

self.driver.find_by_text('查询').click()

sleep(1)

count += 1

print('第%d次点击查询……' % count)

try:

car_no_location = self.driver.find_by_id("queryLeftTable")[0].find_by_text(self.number)[0]

current_tr = car_no_location.find_by_xpath("./../../../../..")

if current_tr.find_by_tag('td')[self.seat_type_index].text == '--':

print('无此座位类型出售,已结束当前刷票,请重新开启!')

sys.exit(1)

elif current_tr.find_by_tag('td')[self.seat_type_index].text == '无':

print('无票,继续尝试……')

elif current_tr.find_by_tag('td')[self.seat_type_index].text == '候补':

print('候补,继续尝试……')

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()

sleep(1)

print('开始选择用户……')

key_value = 1

for p in self.passengers:

# 选择用户

self.driver.find_by_text(p).last.click()

# 选择座位类型

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

sleep(0.5)

if p[-1] == ')':

self.driver.find_by_id('dialog_xsertcj_ok').click()

print('正在提交订单……')

self.driver.find_by_id('submitOrder_id').click()

sleep(0.5)

# 查看放回结果是否正常

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()

sleep(0.2)

self.driver.find_by_id('preStep_id').click()

sleep(0.3)

continue

print('正在确认订单……')

self.driver.find_by_id('submitOrder_id').click()

sleep(1)

print('预订成功,请及时前往支付……')

# 发送内容

def mail_to(neirong, biaoti, geishei):

text = neirong

msg = MIMEText(text, 'plain', 'utf-8')

msg['subject'] = biaoti

msg["From"] = geishei

s = smtplib.SMTP(SMTPServer, 25)

s.login(sender, passwd)

s.sendmail(sender, sender, msg.as_string())

s.quit()

mail_to(neirong, biaoti, geishei)

#self.driver.quit()

except Exception as error_info:

print(error_info)

#self.driver.quit()

break

except Exception as error_info:

print(error_info)

self.driver.quit()

sys.exit(1)

if __name__ == '__main__':

# 城市cookie字典

city_list = {

'bj': '%u5317%u4EAC%2CBJP', # 北京

'hd': '%u5929%u6D25%2CTJP', # 邯郸

'nn': '%u5357%u5B81%2CNNZ', # 南宁

'wh': '%u6B66%u6C49%2CWHN', # 武汉

'cs': '%u957F%u6C99%2CCSQ', # 长沙

'ty': '%u592A%u539F%2CTYV', # 太原

'yc': '%u8FD0%u57CE%2CYNV', # 运城

'gz': '%u5E7F%u5DDE%2CGZQ', # 广州

'qhc': '%u6E05%u6CB3%u57CE%2CQYP' # 清河城

}

#邮件信息

SMTPServer = "smtp.163.com"

sender = "**********@163.com"

passwd = "********"

c = time.time()

b = time.localtime(c)

q = time.strftime("%Y-%m-%d %X", b)

neirong = ("12306:" + q + " 搶到票了,轉進時間吧!")

biaoti = ("一封信 " + q)

geishei = sender

# 从txt中获取信息

with open(r'H:\For personal use\PYTHON\file\tickets.txt', 'r', encoding='utf-8', errors='ignore') as f:

info_array = np.genfromtxt(f, dtype=str, delimiter=':')

account = info_array[0][1]

password = info_array[1][1]

from_time = info_array[2][1]

start = info_array[3][1]

end = info_array[4][1]

from_station = city_list[start]

to_station = city_list[end]

number = info_array[5][1]

seat_type = info_array[6][1]

passengers = info_array[7][1].split(",")

#打印外汇返佣信息

print(account, password, passengers, from_time, from_station, to_station, number, seat_type)

# 开始抢票

ticket = BrushTicket(account, password, passengers, from_time, from_station, to_station, number, seat_type)

ticket.start_brush()

原文链接:https://blog.csdn.net/qq_34158880/article/details/103602447

python自动刷新抢火车票的更多相关文章

  1. jenkins发布程序触发shell调用python脚本刷新akamai cdn api

    刷新cdn的流程:jenkins获取git中的代码,触发脚本推送到生产环境中(即cdn的源站) --> 触发脚本获取git工作目录的更新列表,将更新列表拼凑成带域名信息的url,写入到目录中 - ...

  2. python抢火车票的脚本

    起因: 想着那么多人,抢不到火车票.感觉到一丝感慨 所以有了抢火车票这个脚本. 0x01 思路:自动打开浏览器,自动输入账号密码 知道查看.自动预定. 0x02 要用到的模块 splinter模块: ...

  3. Python用上锁和解锁 lock lock.acquire lock.release 模拟抢火车票

    Python用上锁和解锁  lock lock.acquire lock.release 模拟抢火车票 import jsonimport timefrom multiprocessing impor ...

  4. 使用node-livereload自动刷新页面

    1. 安装node 2. 安装python 3. 安装connect, serve-static和node-livereload (以下都假设命令行当前目录为e:\WebSite) e:\WebSit ...

  5. Python操作12306抢票脚本

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

  6. flask-script实现自动刷新页面调试

    本文flask==1.0.2 1.导入extension包 from flask_script import Manager 2.使用manager管理工具 app = Flask(__name__) ...

  7. BrowserSync(保存代码后,自动刷新浏览器)

    摘要 Browsersync能让浏览器实时.快速响应您的文件更改(html.js.css.sass.less等)并自动刷新页面.更重要的是 Browsersync可以同时在PC.平板.手机等设备下进项 ...

  8. BrowserSync(前端利器—保存代码后,自动刷新浏览器)

    摘要 Browsersync能让浏览器实时.快速响应您的文件更改(HTML.JavaScript.CSS.Sass.Less.PHP.Python等)并自动刷新页面.更重要的是 Browsersync ...

  9. 浏览器自动刷新——基于Nodejs的Gulp LiveReload与VisualStudio完美结合。

    本文版权桂博客园和作者吴双共同所有,转载和爬虫请注明原文地址 http://www.cnblogs.com/tdws/p/6016055.html 写在前面 大家好我是博客园的蜗牛,博客园的蜗牛就是我 ...

随机推荐

  1. UPDATE 在不同数据库中的使用方式

    MYSQL 中update 表一 set Gmoney = 表二.列名 from 表一,表二 where 表一.EMPID = 表二.EMPID举例:update table1 set table1. ...

  2. Period POJ - 1961

    Period POJ - 1961 时限: 3000MS   内存: 30000KB   64位IO格式: %I64d & %I64u 提交 状态 已开启划词翻译 问题描述 For each ...

  3. 使用CFStringTransform将汉字转换为拼音

    之前做通讯录相关的一些App时,有一个比较常用的算法是将汉字转换成拼音.当时采用的做法是:将各个拼音段的首个汉字(按Unicode排序)做成两个数组,一个数组存拼音,另一个数组存拼音对应首个汉字的Un ...

  4. 『转』一千行MySQL学习笔记

    /* 启动MySQL */ net start mysql /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限验证登录MySQL */ mysq ...

  5. MySQL - 修改数据库文件物理路径

    一共两步: 修改my.ini文件的datadir: 将修改前datadir路径下的文件复制到修改后的datadir路径. 注意: my.ini可能有多个,windows 系统可以在 MySQL 服务的 ...

  6. C语言的未初始化的数组的值为什么是随机的

    突然想起来前几天同学问我为什么没有初始化的数组的值是随机的,发现这个困惑自己也是存在的,所以自己总结的心得. 1. 首先,并不是所有未初始化的数组的值都是随机的.对于没有初始化的数组,分两种情况: ( ...

  7. HTML-lang属性规定元素内容的语言

    所有浏览器均支持 lang 属性. 属性lang是英语language的缩写,意思是语言,”en”代表英语,”zh-CN”代表中文 注释:lang 属性在以下标签中无效:<base>, & ...

  8. Maximum Subarray(最大连续子序列和)

    https://leetcode.com/problems/maximum-subarray/ 思路: 如果全为负值,那么取最大值 如果有非负值,那么我们依次计算到当前位置为止的最大值.假设有n个元素 ...

  9. CentOS7和Ubuntu18.10下运行Qt Creator出现cannot find -lGL的问题的解决方案

    解决方法:缺少相应的opengl的库,需要安装opengl库 一.Ubuntu下解决Qt5.11.1 cannot find -lGL 有两种原因: 一种是没有按照libGL库,那么就安装: sudo ...

  10. ELK日志分析系统之logstash7.x最新版安装与配置

    2 .Logstash的简介 2.1 logstash 介绍 LogStash由JRuby语言编写,基于消息(message-based)的简单架构,并运行在Java虚拟机(JVM)上.不同于分离的代 ...