python判断⽕车票座位是否靠窗_Python查询⽕车票(⼀)'''Usage:Query_Ticket.py [agdtkz] [gdtkz]:a 全部d 动车g ⾼铁k 快速t 特快z 直达:year-month-day'''
import requests
from datetime import datetime
from prettytable import PrettyTable
import sys
import Station_Parse
from requests.ptions import InsecureRequestWarning
# 忽视该警告
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
# 获取参数
if len(sys.argv) < 5:
print('Usage: python Query_Ticket.py [agdtkz] ')
ticket_option = sys.argv[1]
from_station = sys.argv[2]
to_station = sys.argv[3]
date = sys.argv[4]
霍建华 叶璇# 数据处理+显⽰
class Trains_Demo():
headers = '车次 车站 时间 历时 商务/特等座 ⼀等座 ⼆等座 ⾼级软卧 软卧 动卧 硬卧 软座 硬座 ⽆座'.split()
# 初始化
def __init__(self, raw_trains, option):
self.raw_trains = raw_trains
self.option = option
# 获取出发和到达站
def get_from_to_station_name(self, data_list):
self.from_station_name = data_list[6]
<_station_name = data_list[7]
self.from_to_station_name = Station_Parse.parse_station().disparse(self.from_station_name) + '-->' +
Station_Parse.parse_station()._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': _from_to_station_name(data_list), 'start_arrive_time': _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 '--',
# 'others': data_list[34] or '--'
}
# 判断是否需要显⽰
def need_show(self, data_list):
trips = data_list[3]
initial = trips[0].lower()
黄涵女儿if self.option == 'a':
return trips
else:
return(initial in self.option)
# 数据显⽰
def show_trian_data(self):
self.demo = PrettyTable()
self.demo._set_field_names(self.headers)
ain in self.raw_trains:灰空间我是谁
self.data_list = ain.split('|')
d_show(self.data_list):
self.values_row = []
self.parsed_train_data = self.parse_trains_data(self.data_list)
self.values_row.append(self.parsed_train_data['trips'])
self.values_row.append(self.parsed_train_data['from_to_station_name']) self.values_row.append(self.parsed_train_data['start_arrive_time'])
self.values_row.append(self.parsed_train_data['duration'])
self.values_row.append(self.parsed_train_data['business_premier_seat']) self.values_row.append(self.parsed_train_data['first_class_seat'])
self.values_row.append(self.parsed_train_data['second_class_seat']) self.values_row.append(self.parsed_train_data['senior_soft_sleep']) self.values_row.append(self.parsed_train_data['soft_sleep'])张柏芝原图
self.values_row.append(self.parsed_train_data['move_sleep'])
self.values_row.append(self.parsed_train_data['hard_sleep'])
self.values_row.append(self.parsed_train_data['soft_seat'])
self.values_row.append(self.parsed_train_data['hard_seat'])
self.values_row.append(self.parsed_train_data['no_seat'])
# self.values_row.append(self.parsed_train_data['others'])
self.demo.add_row(self.values_row)
print(self.demo)
# 车票查询
class Query_Ticket(object):
# 请求地址的模板
url_template = (
'kyfw.12306/otn/leftTicket/query{}?leftTicketDTO.'
'train_date={}&'
'leftTicketDTO.from_station={}&'
'_station={}&'
胜利事件'purpose_codes=ADULT'
)
# 初始化
def __init__(self):
self.ticket_option = ticket_option
self.from_station = Station_Parse.parse_station().parse(from_station)
<_station = Station_Parse.parse_station().parse(to_station)
self.date = date
if self.from_station is None _station is None:
print('请输⼊有效车站名...')
exit()
try:
if datetime.strptime(self.date, '%Y-%m-%d') < w():
raise ValueError
except:
print('请输⼊有效⽇期...')
exit()
# 获得请求地址
def request_url1(self):
return(self.url_template.format('A', self.date, self.from_station, _station))
def request_url2(self):
return(self.url_template.format('Z', self.date, self.from_station, _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'}
try:
except:
百里挑一谢梦Trains_ains, self.ticket_option).show_trian_data()
if __name__ == '__main__':
Query_Ticket().query()
发布评论