12306余票查询
一直想做这么一个项目,但是之前一直打不开12306返回信息网页的页面,后来才发现是因为没有加上请求头。12306一旦发现你不是浏览器,就不会返回页面。但是请求方式还是get,加个请求头伪造成浏览器就行了。另外车站代码与车站名称的关联就不多加叙述了。
此外还碰到许多问题,比如colorama模块显示不了颜色、返回的车站信息还有停运车次的。
下面是码源,初学python,写法粗糙,还请大家指正。
from tkinter import *
from prettytable import PrettyTable
from tkinter import scrolledtext
from colorama import Back,Fore,Style#用不了!!QAQ
import requests,re
import tkinter.messagebox
From_station =[]
To_station = []
Time = []
dict_station = {}
dict1 = {}
table = PrettyTable(['Trips','departure time/Time of arrival','Business seat','First-class seat','Second class','Senior soft sleeper','First-sleeper','hard sleeper','hard seat','no-seat'])
url = ' https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date=%s&leftTicketDTO.from_station=%s&leftTicketDTO.to_station=%s&purpose_codes=ADULT'
headers = {
'Host':'kyfw.12306.cn',
'Connection':'keep-alive',
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.1 Safari/605.1.15',
'Cookie':'JSESSIONID=94BF6626E334C88E7721315F29F0FCB8; _jc_save_fromDate=2020-03-17; _jc_save_fromStation=%u957F%u6C99%u5357%2CCWQ; _jc_save_toDate=2020-03-10; _jc_save_toStation=%u90D1%u5DDE%u4E1C%2CZAF; _jc_save_wfdc_flag=dc; route=c5c62a339e7744272a54643b3be5bf64; BIGipServerotn=284164618.24610.0000; BIGipServerpool_passport=283968010.50215.0000; RAIL_DEVICEID=qj0QrUHQL56afSGSRIRfreY1nMj6oGTs0_BUW4IzhQc1lSQLE92lBPcPBmOmo3Tfmb1f0PWBPoB2shMe-r47-kDkgX7ZuS30xtmZlD-6dM2izNvpfaQXetF0W8UkVAn_vBgPhFhd00FeLgvuZXT9V5AJRz-_zV3K; RAIL_EXPIRATION=1584003782555',
}
def window1():
def station(sta):
def check():
url = ' https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.9141'
response = requests.get(url)
response.encoding = 'utf-8'
html = response.text
reg = r'([\u4e00-\u9fa5]+)\|([A-Z]+)' # 正则提取汉子和大写字母
result = re.findall(reg, html)
return result
for station in check():
dict_station[station[0]] = station[1]
return dict_station['%s' % sta]
def help():
tkinter.messagebox.showinfo('帮助','时间格式形如2020-03-20')
def dataprepare():
def getdata():
time = entry1.get()
from_station_1 = entry2.get()
from_station = station(from_station_1)
to_station_1 = entry3.get()
to_station = station(to_station_1)
Time.append(time)
From_station.append(from_station_1)
To_station.append(to_station_1)
response = requests.get(url % (time, from_station, to_station), headers=headers)
html = response.json()
return html['data']['result']
for i in getdata():
tem_list = i.split('|')
dict1['a'] = tem_list[3]
dict1['b'] = tem_list[8] + '\n' + tem_list[9]
if tem_list[23] != '无' and tem_list[23] != '':
dict1['g'] = 'Have Tickets'
else:
dict1['g'] = '--'
if tem_list[28] != '无' and tem_list[28] != '':
dict1['h'] = 'Have Tickets'
else:
dict1['h'] = '--'
if tem_list[26] != '无' and tem_list[26] != '':
dict1['j'] = 'Have Tickets'
else:
dict1['j'] = '--'
if tem_list[21] != '无' and tem_list[21] != '':
dict1['f'] = 'Have Tickets'
else:
dict1['f'] = '--'
if tem_list[29] != '无' and tem_list[29] != '':
dict1['i'] = 'Have Tickets'
else:
dict1['i'] = '--'
if tem_list[30] != '无' and tem_list[30] != '':
dict1['e'] = 'Have Tickets'
else:
dict1['e'] = '--'
if tem_list[31] != '无' and tem_list[31] != '':
dict1['d'] = 'Have Tickets'
else:
dict1['d'] = '--'
if tem_list[32] != '无' and tem_list[32] != '':
dict1['c'] = 'Have Tickets'
else:
dict1['c'] = '--'
table.add_row([dict1['a'], dict1['b'], dict1['c'], dict1['d'], dict1['e'], dict1['f'], dict1['g'], dict1['h'],dict1['i'],dict1['j']])
del dict1['a'], dict1['b'], dict1['c'], dict1['d'], dict1['e'], dict1['f'], dict1['g'], dict1['h'], dict1['i'],dict1['j']
quitmywindow()
def quitmywindow():
myWindow1.destroy()
window2()
myWindow1 = Tk()
Label(myWindow1,text='请输入您的出发日期').grid(row=0,column=0)
Label(myWindow1,text='请输入您的出发地').grid(row=1,column=0)
Label(myWindow1,text='请输入您的目的地').grid(row=2, column=0)
entry1 = Entry(myWindow1)
entry1.grid(row=0,column=1)
entry2 = Entry(myWindow1)
entry2.grid(row=1,column=1)
entry3 = Entry(myWindow1)
entry3.grid(row=2,column=1)
Button(myWindow1, text='退出', command=myWindow1.quit, width=10).grid(row=0, column=2, sticky=W, padx=5, pady=5)
Button(myWindow1, text='帮助',command=help, width=10).grid(row=1, column=2, sticky=W, padx=5, pady=5)
Button(myWindow1, text='确定',command=dataprepare ,width=10).grid(row=2, column=2, sticky=W, padx=5, pady=5)
myWindow1.mainloop()
def window2():
# 初始化Tk()
myWindow1 = Tk()
myWindow1.geometry('1500x800')
# 设置标题
myWindow1.title('余票查询')
# 标签控件布局
# Entry控件布局
text1 = scrolledtext.ScrolledText(myWindow1,width=178,height=60)
text1.grid(row=0, column=0)
text1.insert(INSERT,table)
text2 = scrolledtext.ScrolledText(myWindow1,height=60,width=20)
text2.grid(row=0,column=1)
text2.insert(INSERT,'出发时间:%s\n出发地:%s\n目的地:%s\n\n\n'%(Time[0],From_station[0],To_station[0]))
text2.insert(INSERT,'温馨提示:为了保证准确性,请忽略进站时间与到达时间均为24:00的车次。\n\n\n车次:\nTrips\n软卧一等卧:\nFirst-sleeper\n硬卧二等卧:\nhard sleeper\n无座:\nno-seat\n高级软卧:\nSenior soft sleeper\n硬座:\nhard seat\n二等座包座:\nSecond class\n商务座:\nBusiness seat\n一等座:\nFirst-class seat\n'
)
Button(myWindow1, text='退出', command=myWindow1.quit, width=20).grid(row=3, column=0, sticky=W, padx=5, pady=5)
myWindow1.mainloop()
window1()
这里附上效果图: