import requests#import pandas as pdimport jsonimport prettytable as ptimport datetimecurr_time=datetime.datetime.now()time = datetime.datetime.strftime(curr_time, '%Y-%m-%d %H:%M:%S')print(f' 现在是:{time} by琴棋书画' )print()print('*******欢迎使用12306火车票查票小工具**********')print()f = open('city.json', mode='r', encoding='utf-8')text = f.read()city_json = json.loads(text)#print(city_json)while True:from_station = input('请输入你出发的城市:')to_station = input('请输入你达到的城市:')trAIn_date = input('请输入乘车时间(2021-09-08/年-月-日):')headers = {'Accept': '*/*','Accept-Encoding': 'gzip, deflate, br','Accept-Language': 'zh-CN,zh;q=0.9','Cache-Control': 'no-cache','Cookie': '_uab_collina=166823523003171992715295; JSESSIONID=584E9E13FAD14A4CC1246A0886EAEBCD; BIGipServerotn=200278538.64545.0000; BIGipServerpool_passport=48497162.50215.0000; RAIL_EXPIRATION=1668496753621; RAIL_DEVICEID=Rz7stbJ-4FJ1lH0h3D8urQ9iA_ilqp_rpSpiEnbc1MD7V517p-MczHOcGV-fhjuFvFEXOSxJd2n-FXhT3MVuk3DLhbCva_aFmq9Fp7b144wKWGPL_jdNMpmSmFmMapinWnUr-sDjGDCbpM7bFBBHc1PsD74ZdBba; guidesStatus=off; highContrastMode=defaltMode; cursorStatus=off; fo=a6mja8f0tfgaaysa8AAdkRbNDzBnhpW6al55sUUJuwkUfw2amN5S_gdVtbuYjhyVqom5bGD-e-PTBlehcVSEmk_haH2hSpa-zlQwLR0c1xzAOeD_l2zTXesDTQm-y4_mq-7fHsje5FzgGdQE5Sq8bbp0O-kgGDMIIDnznyR6jogSfCCTl58gJr_xVG0; route=c5c62a339e7744272a54643b3be5bf64; _jc_save_fromStation=%u5317%u4EAC%2CBJP; _jc_save_toStation=%u5B89%u9633%2CAYF; _jc_save_fromDate=2022-11-12; _jc_save_toDate=2022-11-12; _jc_save_wfdc_flag=dc','Host': 'kyfw.12306.cn','If-Modified-Since': '0','Referer': 'https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc&fs=%E5%8C%97%E4%BA%AC,BJP&ts=%E5%AE%89%E9%98%B3,AYF&date=2022-11-12&flag=N,N,Y','User-Agent': 'Mozilla/5.0 (windows NT 6.1; WOW64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/78.0.3904.108 Safari/537.36','X-Requested-With': 'XMLHttpRequest537.36'url = f'https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date={train_date}&leftTicketDTO.from_station={city_json[from_station]}&leftTicketDTO.to_station={city_json[to_station]}&purpose_codes=ADULT'response = requests.get(url=url, headers=headers)print(response)result = response.json()['data']['result']tb=pt.PrettyTable()tb.field_names=['序号','车次','出发时间','到达时间','途经时长','特等座','一等座','二等座','硬座','软座','无座','硬卧','软卧']page=0for index in result:# requests.utils.unquote(index)index_list = index.split('|')# print(index_list)train_number = index_list[3] # 车次time_1 = index_list[8] # 出发时间time_2 = index_list[9] # 到达时间time_3=index_list[10]prince_seat = index_list[32] # 特等座first_class_seat = index_list[31] # 一等座second_class = index_list[30] # 二等座hard_seat = index_list[29] # 硬座soft_seat = index_list[24] # 软座no_seat = index_list[26] # 无座soft_sleeper = index_list[23] # 软卧hard_sleeper = index_list[28] # 硬卧dit = {'Num': train_number,'Start': time_1,'End': time_2,'long':time_3,'Top': prince_seat,'First':first_class_seat,'Second': second_class,'Yz': hard_seat,'rz':soft_seat,'Wz': no_seat,'Rw': soft_sleeper,'Yw': hard_sleeper,tb.add_row([page,train_number,time_1,time_2,time_3,prince_seat,first_class_seat,second_class,hard_seat,soft_seat,no_seat,soft_sleeper,hard_sleeper])page+=1print(tb)again = input('是否继续查询(Y/N): ')if again == 'Y' or again == 'y':continueelse:break