1. 程式人生 > >python +tkinter 12306網站車票查詢系統,第二版

python +tkinter 12306網站車票查詢系統,第二版

經過分析車座位,已得出各種資料

#['預訂', '6i000G103601', 'G1037', 'IOQ', 'HAN', 'HKN', 'TNN', '20:46', '21:21', '00:35', 'Y', 'VIaw4o5W90E7Zr2HTHJJDwDBVnmkp7AKaqj%2B4ZUg4ICQ2CeD', '20180713', '3', 'Q9', '12', '13', '0', '0', '', '', '', '', '', '', '', '', '', '', '7', '有', '無', '', 'O0M090', 'OM9', '0']

 #商務座31  一等座30 二等座29  高軟20 軟臥22 動臥 硬臥27 軟座23 硬座28 無座25 其他21

#{'D633': ['HKN', 'TNN', '07:00', '07:35', '00:35', '', '有', '有', '', '', '', '', '', '', '20', ''],

def get_train_res(self):
    #商務座31  一等座30 二等座29  高軟20 軟臥22 動臥 硬臥27 軟座23 硬座28 無座25 其他21
try:
        res=requests.get(self.url,headers=self.header).text
        res=res.encode(encoding='utf-8').decode('utf-8')
        dic=json.loads(res)
        train_lis=dic["data"
]["result"] for i in train_lis: info=i[i.find("|")+1:] result=info.split("|") self.train_dic[result[2]]=[result[5],result[6],result[7],result[8],result[9], result[24] or result[31],result[30],result[29],result[20], result[22
],result[26],result[27],result[23],result[28],result[25],result[21]] # print(self.train_dic) return self.train_dic except Exception: print("資料錯誤")




經過修改,後的介面寬了些許,列寬是根據字元長度來自動獲取的

def add_train_info(self):
    lis_train=["C"+str(x) for x in range(0,15)]
    tuple_train=tuple(lis_train)
    self.tree=Treeview(self.win,columns=tuple_train,height=30,show="headings")
    self.tree.place(x=168,y=40,width=812,height=350)
    train_info=[' 車次 ',' 出發/到達站','出發/到達時間','歷時 ','商/特座','一等座','二等座','高軟 ','軟臥 ','動臥 ','硬臥 ','軟座 ','硬座 ','無座 ','其他']
    for i in range(0,len(lis_train)):
        self.tree.column(lis_train[i],width=len(train_info[i])*11,anchor='center')#列寬是字元長度*11,比較合適
        self.tree.heading(lis_train[i], text=train_info[i])

判斷輸入日期:

def is_leapyear(self):
    #先判斷輸入是否是日期,如果是日期執行方法體,
a=self.C_year.get()
    b=self.C_mon.get()
    c=self.C_day.get()
    pa_year = '20[\d][\d]'  # 2018
if re.compile(pa_year).findall(a) and b in ["{:02d}".format(x) for x in range(1, 13)] and c in [
        "{:02d}".format(x) for x in range(1, 32)]:
        nowtime = time.localtime()
        now_time_sp = time.mktime(nowtime)
        start_time=a+"-"+b+"-"+c+" 23:59:29" #"2018-08-09  23:59:29"
start_timestrip = time.strptime(start_time, "%Y-%m-%d %H:%M:%S")
        start_times = time.mktime(start_timestrip)
        days=(start_times-now_time_sp)/60/60/24
print(days)
        print(a,b,c)
        if days>29:
            messagebox.showerror(title="警告",message="大於30天無法獲取資料")
        elif days<0:
            messagebox.showerror(title="警告",message="小於1天無法獲取資料")
        elif days>0 and days<30:
            if int(a) % 4 == 0 and int(a) % 100 != 0 or int(a) % 400 == 0:#如果是閏年
if (int(b) in (1,3,5,7,8,10,12) and int(c)>31) or ((int(b) in (4,6,9,11) and int(c)>30)) or (int(b)==2 and int(c)>29):
                    messagebox.showerror(title="警告",message="你確定這個月有這一天麼")
            else:
                if (int(b) in (1,3,5,8,10,12) and int(c)>31) or ((int(b) in (4,6,9,11) and int(c)>30)) or (int(b)==2 and int(c)>28):
                    messagebox.showerror(title="警告",message="你確定這個月有這一天麼")
    else:
        messagebox.showerror(title="警告", message="請輸入正確格式的年:月:日")
剩下的程式碼請到github獲取