########################################################
# Python学习交流群:548377875
#源码+微信:mmp9972
# 火车票抢票系统V1.0
########################################################
import requests
import Station_Parse
import threading
import os
from datetime import datetime
from splinter.browser import Browser
from time import sleep
from tkinter import *
from PIL import Image, ImageTk
from tkinter import messagebox
from requests.packages.urllib3.exceptions import InsecureRequestWarning
# 忽视该警告
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
# 车票查询部分
#-------------------------------------------------------------------------------------------------------------------------------------------
# 数据处理+显示
class Trains_Demo():
# 初始化
def __init__(self, txt_show_ticket, raw_trains, option):
self.headers = '车次: 车站: 时间: 历时: 商务/特等座: 一等座: 二等座: 高级软卧: 软卧: 动卧: 硬卧: 软座: 硬座: 无座:'.split()
self.raw_trains = raw_trains
self.option = option
txt_show_ticket.delete(1.0, END)
# 获取出发和到达站
def get_from_to_station_name(self, data_list):
self.from_station_name = data_list[6]
self.to_station_name = data_list[7]
self.from_to_station_name = Station_Parse.parse_station().disparse(self.from_station_name) + '-->' + Station_Parse.parse_station().disparse(self.to_station_name)
return self.from_to_station_name
# 获得出发和到达时间
def get_start_arrive_time(self, data_list):
self.start_arrive_time = data_list[8] + '-->' + data_list[9]
return self.start_arrive_time
# 解析trains数据(与headers依次对应)
def parse_trains_data(self, data_list):
return {
'trips': data_list[3],
'from_to_station_name': self.get_from_to_station_name(data_list),
'start_arrive_time': self.get_start_arrive_time(data_list),
'duration': data_list[10],
'business_premier_seat': data_list[32] or '--',
'first_class_seat': data_list[31] or '--',
'second_class_seat': data_list[30] or '--',
'senior_soft_sleep': data_list[21] or '--',
'soft_sleep': data_list[23] or '--',
'move_sleep': data_list[33] or '--',
'hard_sleep': data_list[28] or '--',
'soft_seat': data_list[24] or '--',
'hard_seat': data_list[29] or '--',
'no_seat': data_list[26] or '--',
}
# 判断是否需要显示
def need_show(self, data_list):
self.trips = data_list[3]
initial = self.trips[0].lower()
if 'a' in self.option:
return self.trips
else:
return(initial in self.option)
# 数据显示
def show_trian_data(self):
self.t_num = 0
for self.train in self.raw_trains:
self.data_list = self.train.split('|')
if self.need_show(self.data_list):
self.values_row = []
self.parsed_train_data = self.parse_trains_data(self.data_list)
self.values_row.append(self.headers[0] + self.parsed_train_data['trips'])
self.values_row.append(self.headers[1] + self.parsed_train_data['from_to_station_name'])
self.values_row.append(self.headers[2] + self.parsed_train_data['start_arrive_time'])
self.values_row.append(self.headers[3] + self.parsed_train_data['duration'])
self.values_row.append(self.headers[4] + self.parsed_train_data['business_premier_seat'])
self.values_row.append(self.headers[5] + self.parsed_train_data['first_class_seat'])
self.values_row.append(self.headers[6] + self.parsed_train_data['second_class_seat'])
self.values_row.append(self.headers[7] + self.parsed_train_data['senior_soft_sleep'])
self.values_row.append(self.headers[8] + self.parsed_train_data['soft_sleep'])
self.values_row.append(self.headers[9] + self.parsed_train_data['move_sleep'])
self.values_row.append(self.headers[10] + self.parsed_train_data['hard_sleep'])
self.values_row.append(self.headers[11] + self.parsed_train_data['soft_seat'])
self.values_row.append(self.headers[12] + self.parsed_train_data['hard_seat'])
self.values_row.append(self.headers[13] + self.parsed_train_data['no_seat'])
self.t_num += 1
txt_show_ticket.insert(END, '第%d班:' % self.t_num + '*'*80)
txt_show_ticket.insert(END, '\n')
txt_show_ticket.insert(END, self.values_row)
txt_show_ticket.insert(END, '\n')
# 车票查询
class Query_Ticket():
# 初始化
def __init__(self, txt_show_ticket, ticket_option, from_station, to_station, date):
# 请求地址的模板
self.url_template = (
'https://kyfw.12306.cn/otn/leftTicket/query{}?leftTicketDTO.'
'train_date={}&'
'leftTicketDTO.from_station={}&'
'leftTicketDTO.to_station={}&'
'purpose_codes=ADULT'
)
self.ticket_option = ticket_option
self.from_station = from_station
self.to_station = to_station
self.date = date
# 获得请求地址
def request_url1(self):
return(self.url_template.format('A', self.date, self.from_station, self.to_station))
def request_url2(self):
return(self.url_template.format('Z', self.date, self.from_station, self.to_station))
# 查询车票
def query(self):
self.headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3294.6 Safari/537.36'}
self.res = requests.get(self.request_url1(), headers=self.headers, verify=False)
try:
try:
self.trains = self.res.json()['data']['result']
except:
self.res = requests.get(self.request_url2(), headers=self.headers, verify=False)
self.trains = self.res.json()['data']['result']
Trains_Demo(txt_show_ticket, self.trains, self.ticket_option).show_trian_data()
except:
title = '提示'
message = '出了点小问题,请重新点击按钮!'
messagebox.showinfo(title, message)
#-------------------------------------------------------------------------------------------------------------------------------------------
# 抢票部分
#-------------------------------------------------------------------------------------------------------------------------------------------
# 抢票
class Buy_Tickets(object):
# 初始化
def __init__(self, username, passwd, order, passengers, dtime, starts, ends):
self.url_template = (
'https://kyfw.12306.cn/otn/leftTicket/query{}?leftTicketDTO.'
'train_date={}&'
'leftTicketDTO.from_station={}&'
'leftTicketDTO.to_station={}&'
'purpose_codes=ADULT'
)
self.username = username
self.passwd = passwd
# 日期
self.dtime = dtime
# 乘客名
self.passengers = passengers.split(',')
# 起始地和终点
self.starts = Station_Parse.parse_station().parse(starts)
self.ends = Station_Parse.parse_station().parse(ends)
if self.starts is None or self.ends is None:
self.title = '提示'
self.message = '请输入有效的车站名'
messagebox.showinfo(self.title, self.message)
# 车次
self.order = order
if self.order != '0':
self.order = self.order_transfer()
while(not self.order):
sleep(1)
self.order = self.order_transfer()
# 起始地和终点转为cookie值
self.starts = self.Get_Cookies()[0] + '%2C' + self.starts
self.ends = self.Get_Cookies()[1] + '%2C' + self.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 = 'chrome'
self.executable_path = 'D:\\Python35\\Scripts\\chromedriver.exe'
# 登录
def login(self):
self.driver.visit(self.login_url)
self.driver.fill('loginUserDTO.user_name', self.username)
self.driver.fill('userDTO.password', self.passwd)
self.title = '提示'
self.message = '请在自动打开的浏览器中输入验证码!'
messagebox.showinfo(self.title, self.message)
while True:
if self.driver.url != self.initMy_url:
sleep(1)
else:
br
静香是个程序媛
- 粉丝: 4217
- 资源: 563
最新资源
- APF有源滤波器仿真,三相三线制,谐波电流检测模块p-q,ip-iq,控制模块pwm滞环算法
- 基于ThinkPHP 5.1框架的lin-cms-tp5简易实用CMS设计源码
- 基于CIM框架修改的IM系统设计源码
- 基于Java语言的tundra-base平台设计源码
- 基于Java核心技术的EXT版招标后台设计源码
- 开关频率可调的永磁同步电机svpwm发电仿真模型,可调稳定发电电压,负载,母线电容可调,可用于设计母线电容,可看输出纹波大小
- 基于Java语言的sass财务软件后端设计源码
- 基于Java语言及类似mybatisPlus Lambda的MongoDB CRUD操作简化设计源码
- 基于Python的家居仓库管理设计源码
- pipline-sar adc 12bit 6?8结构 带有冗余 异步时许,含有gainboost 放大器作为级间放大器 pip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈