![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38CXlZHbvN3cpR2Lc1TPB10QGtWUCpEMJ9CXsxWam9CXwADNvwVZ6l2c052bm9CXUJDT1wkNhVzLcRnbvZ2Lc1TPRF2a1cVYvJ1MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2LcRHelR3LcJzLctmch1mclRXY39DMyYDNxQzMzIDOwcDM4EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
经过分析车座位,已得出各种数据
#['预订', '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获取