python36 +tkinter制作12306火车票查询系统

12306是个令人头疼的网站,用python写了一个查询车次的爬虫

请移步 12306爬虫第二版 12306爬虫更新车票信息




12306车次 G  T  车次的余票信息规则诡异,比如


1461车次    0|0|后面的四个|分割 对应的是商务座-高级软卧,有 后面又是2个分隔符对应的是哪个座位


|20180713|3|P2|01|27|0|0||||有|||有||有|有|||||10403010|1431|0",

G129  又比如这个  1|0  后面 10个分隔符 车次里只有8个-符,后面的2个无对应的是特等座和一等座


|20180713|3|P4|01|11|1|0|||||||||||有|无|无||O0M090|OM9|0"

G127


|20180713|3|P3|01|10|1|0|||||||||||有|有|5||O0M090|OM9|0"

经过和大神交流,已提取出车票数据,

关键代码如下:

def get_tarin_ticket(self):#车票 商务A9 P 一等M 二等O 高软A6 软卧A4 动卧F 硬卧A3 软座A2 硬座A1 无座WZ 其他
    new_tick_dic=self.train_dic.copy()temp_list=[]tick_lg=["A9","P","M","O","A6","A4","F","A3","A2","A1","WZ"]for i,j in new_tick_dic.items():new_tick_dic[i]=[j[0],j[1],j[2],j[3],j[4],tick_lg[0] or tick_lg[1],tick_lg[2],tick_lg[3],tick_lg[4],tick_lg[5],tick_lg[6],tick_lg[7],tick_lg[8],tick_lg[9],tick_lg[10]]# print(self.new_tick_dic)
    for i in self.train_agrs:a,b,c,d,e=iurl='https://kyfw.12306.cn/otn/leftTicket/queryTicketPrice?train_no={}&from_station_no={}' \'&to_station_no={}&seat_types={}&train_date={}'.format(a,b,c,d,e)res=requests.get(url,headers=self.header).content.decode('utf-8')tick_dic=json.loads(res)self.ticks_dic=tick_dic["data"]temp_list.append(self.ticks_dic)# print(temp_list)
    new_train_tick_dic=self.creat_new_dic(new_tick_dic,temp_list)print(new_train_tick_dic)return new_train_tick_dic
lis列表就是上面 temp_list列表

比如:先前提取的车次信息字典列表

dic={'D633': ['HKN', 'TNN', '07:00', '07:35', '00:35', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D5762': ['WCN', 'TNN', '07:25', '08:11', '00:46', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D5885': ['HKN', 'TNN', '08:13', '08:48', '00:35', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D5973': ['HKN', 'TNN', '08:18', '08:59', '00:41', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D2277': ['HKN', 'TNN', '08:36', '09:11', '00:35', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D5953': ['HKN', 'TNN', '08:54', '09:29', '00:35', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D7083': ['HKN', 'TNN', '09:04', '09:39', '00:35', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D7063': ['HKN', 'TNN', '09:14', '09:49', '00:35', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D629': ['HKN', 'TNN', '09:28', '10:09', '00:41', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D5993': ['HKN', 'TNN', '09:47', '10:22', '00:35', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D5853': ['HKN', 'TNN', '09:57', '10:32', '00:35', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D5752': ['WCN', 'TNN', '10:20', '11:05', '00:45', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D3253': ['HKN', 'TNN', '10:50', '11:25', '00:35', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'G2031': ['HKN', 'TNN', '11:00', '11:35', '00:35', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'K1256': ['WCN', 'TMN', '11:13', '12:51', '01:38', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D2255': ['HKN', 'TNN', '11:42', '12:17', '00:35', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D3073': ['HKN', 'TNN', '12:39', '13:14', '00:35', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D2223': ['HKN', 'TNN', '12:52', '13:27', '00:35', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D5875': ['HKN', 'TNN', '13:30', '14:06', '00:36', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D7031': ['HKN', 'TNN', '14:16', '14:57', '00:41', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'K8114': ['WCN', 'TMN', '15:05', '16:53', '01:48', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D2271': ['HKN', 'TNN', '15:28', '16:05', '00:37', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D2233': ['HKN', 'TNN', '15:35', '16:16', '00:41', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D2227': ['HKN', 'TNN', '15:45', '16:26', '00:41', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'G315': ['HKN', 'TNN', '15:54', '16:35', '00:41', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D2377': ['HKN', 'TNN', '16:03', '16:44', '00:41', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D5863': ['HKN', 'TNN', '16:18', '16:53', '00:35', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D5977': ['HKN', 'TNN', '16:40', '17:26', '00:46', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'G555': ['HKN', 'TNN', '17:25', '18:00', '00:35', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D3081': ['HKN', 'TNN', '17:50', '18:25', '00:35', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'G1033': ['HKN', 'TNN', '18:19', '18:54', '00:35', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D5823': ['HKN', 'TNN', '18:45', '19:20', '00:35', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D5776': ['WCN', 'TNN', '19:16', '20:01', '00:45', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D7071': ['HKN', 'TNN', '19:50', '20:25', '00:35', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'D5827': ['HKN', 'TNN', '19:56', '20:37', '00:41', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ'], 'G1037': ['HKN', 'TNN', '20:46', '21:21', '00:35', 'A9', 'M', 'O', 'A6', 'A4', 'F', 'A3', 'A2', 'A1', 'WZ']}
lis=[{'OT': [], 'WZ': '¥29.0', 'M': '¥47.0', 'O': '¥29.0', 'train_no': '4e0000D63207'}, {'OT': [], 'WZ': '¥36.0', 'M': '¥57.0', 'O': '¥36.0', 'train_no': '39000D576210'}, {'OT': [], 'WZ': '¥20.0', 'M': '¥32.0', 'O': '¥20.0', 'train_no': '39000D58850B'}, {'OT': [], 'WZ': '¥23.0', 'O': '¥23.0', 'train_no': '4e000D59720A'}, {'OT': [], 'WZ': '¥31.0', 'M': '¥49.0', 'O': '¥31.0', 'train_no': '4e000D227608'}, {'OT': [], 'WZ': '¥31.0', 'M': '¥49.0', 'O': '¥31.0', 'train_no': '39000D595206'}, {'OT': [], 'WZ': '¥31.0', 'M': '¥49.0', 'O': '¥31.0', 'train_no': '39000D708310'}, {'OT': [], 'WZ': '¥31.0', 'M': '¥49.0', 'O': '¥31.0', 'train_no': '39000D706201'}, {'OT': [], 'WZ': '¥31.0', 'M': '¥49.0', 'O': '¥31.0', 'train_no': '390000D62902'}, {'OT': [], 'WZ': '¥31.0', 'M': '¥49.0', 'O': '¥31.0', 'train_no': '39000D599205'}, {'OT': [], 'WZ': '¥31.0', 'M': '¥49.0', 'O': '¥31.0', 'train_no': '4e000D58520A'}, {'OT': [], 'WZ': '¥36.0', 'M': '¥57.0', 'O': '¥36.0', 'train_no': '39000D575201'}, {'OT': [], 'WZ': '¥26.0', 'M': '¥42.0', 'O': '¥26.0', 'train_no': '5u000D325204'}, {'OT': [], 'WZ': '¥31.0', 'M': '¥49.0', 'A9': '¥92.0', '9': '920', 'O': '¥31.0', 'train_no': '38000G203120'}, {'3': '655', 'A1': '¥19.5', '1': '195', 'A4': '¥99.5', 'A3': '¥65.5', '4': '995', 'OT': [], 'WZ': '¥19.5', 'train_no': '5a000K125690'}, {'OT': [], 'WZ': '¥31.0', 'M': '¥49.0', 'O': '¥31.0', 'train_no': '5s000D225430'}, {'A6': '¥330.0', 'OT': [], 'WZ': '¥31.0', 'M': '¥49.0', 'O': '¥31.0', 'train_no': '5l000D307261'}, {'OT': [], 'WZ': '¥31.0', 'M': '¥49.0', 'O': '¥31.0', 'train_no': '56000D222252'}, {'OT': [], 'WZ': '¥31.0', 'M': '¥49.0', 'O': '¥31.0', 'train_no': '39000D587500'}, {'OT': [], 'WZ': '¥31.0', 'M': '¥49.0', 'O': '¥31.0', 'train_no': '39000D703100'}, {'3': '675', 'A1': '¥21.5', '1': '215', 'A3': '¥67.5', 'OT': [], 'WZ': '¥21.5', 'train_no': '39000K811406'}, {'OT': [], 'WZ': '¥31.0', 'M': '¥49.0', 'O': '¥31.0', 'train_no': '5l000D227130'}, {'OT': [], 'WZ': '¥31.0', 'M': '¥49.0', 'O': '¥31.0', 'train_no': '5n000D22320E'}, {'OT': [], 'WZ': '¥31.0', 'M': '¥49.0', 'O': '¥31.0', 'train_no': '58000D22260B'}, {'P': '¥52.0', 'OT': [], 'WZ': '¥29.0', 'M': '¥47.0', 'O': '¥29.0', 'train_no': '490000G31808'}, {'OT': [], 'WZ': '¥31.0', 'M': '¥49.0', 'O': '¥31.0', 'train_no': '58000D237603'}, {'OT': [], 'WZ': '¥31.0', 'M': '¥49.0', 'O': '¥31.0', 'train_no': '4e000D586209'}, {'OT': [], 'WZ': '¥31.0', 'M': '¥49.0', 'O': '¥31.0', 'train_no': '39000D597720'}, {'OT': [], 'WZ': '¥31.0', 'M': '¥49.0', 'A9': '¥92.0', '9': '920', 'O': '¥31.0', 'train_no': '240000G55506'}, {'OT': [], 'WZ': '¥31.0', 'M': '¥49.0', 'O': '¥31.0', 'train_no': '5l000D308150'}, {'OT': [], 'WZ': '¥29.0', 'M': '¥47.0', 'A9': '¥87.0', '9': '870', 'O': '¥29.0', 'train_no': '6i000G10320A'}, {'OT': [], 'WZ': '¥31.0', 'M': '¥49.0', 'O': '¥31.0', 'train_no': '39000D582320'}, {'OT': [], 'WZ': '¥36.0', 'M': '¥57.0', 'O': '¥36.0', 'train_no': '39000D577601'}, {'OT': [], 'WZ': '¥23.0', 'M': '¥37.0', 'O': '¥23.0', 'train_no': '39000D707101'}, {'OT': [], 'WZ': '¥31.0', 'M': '¥49.0', 'O': '¥31.0', 'train_no': '39000D582730'}, {'OT': [], 'WZ': '¥20.0', 'M': '¥32.0', 'A9': '¥60.0', '9': '600', 'O': '¥20.0', 'train_no': '6i000G103601'}]new_dic={}
new_lis = [[] for x in range(len(lis))]
def creat_new_dic(dic,lis):j=list(dic.values())for i in range(len(j)):# i=0-36
        for num in range(len(j[i])):#num  0-15
            if num>4:if lis[i].get(j[i][num]):new_lis[i].append(lis[i].get(j[i][num]))else:new_lis[i].append("-")else:new_lis[i].append(j[i][num])for i in range(len(j)):new_dic[list(dic.keys())[i]]=new_lis[i]print(new_dic)
creat_new_dic(dic,lis)
输出的类型如下:
'D633': ['HKN', 'TNN', '07:00', '07:35', '00:35', '-', '¥47.0', '¥29.0', '-', '-', '-', '-', '-', '-', '¥29.0']

 



本文链接:https://my.lmcjl.com/post/16432.html

展开阅读全文

4 评论

留下您的评论.