1.首先从如何查票开始做
qeury_ticket.py获取车次在程序中的简写
station.py 查询车票
station.py
# -*- coding: utf-8 -*-
'''
该文件主要是获取车站的信息
'''
import re,requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
url = r"https://kyfw.12306.cn/otn/resources/js/framework/station_name.js"
response = requests.get(url,verify=False)
station_name=(response.content.split('='))[1]
list_name= station_name.split('@')
station_list=[]
for x in list_name:
if(len(x)>1):
station_list.append(x)
for x in station_list:
print x
pass
qeury_ticket.py
```python
# -*- coding: utf-8 -*-
import re,requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
import json
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
def CheCi_(sd):
CheCi_info={'VAP':'北京南','HGH':'杭州东','HZH':'杭州','XHH':'杭州南','EAY':'西安北','XAY':'西安','CAY':'西安南','LAJ':'兰州西','WAR':'乌鲁木齐','BTC':'包头','LZJ':'兰州'}
return CheCi_info[sd]
def main():
# date = input("请输入时间(如2019-01-22):\n")
# from_station = chezhan_code[input("请输入起始站点:\n")]
# to_station = chezhan_code[input("请输入目的站点:\n")]
url = "https://kyfw.12306.cn/otn/leftTicket/query?"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.5702.400 QQBrowser/10.2.1893.400",
"cookie:":"JSESSIONID=7C7B35F054AFC3E9440A54174A883962; RAIL_EXPIRATION=1574757030659; RAIL_DEVICEID=n7VRfs6VMJKnQEWro3noLaeORZ1Fgsw5c4UzYCVdz1hD8w2iL2HAUSODY2hUqXbxC4ThOa1w1SlSJ1mVVeAdedwQg0v1ozxb-u8Gd3HsNdFYeWO8BJ-weUPvKB89F-QMM1WxIvamOialf9CDkB3J9kXD-NebnHoV; _jc_save_fromStation=%u897F%u5B89%2CXAY; _jc_save_toStation=%u676D%u5DDE%2CHZH; _jc_save_fromDate=2019-11-23; _jc_save_toDate=2019-11-23; _jc_save_wfdc_flag=dc; BIGipServerotn=233832970.24610.0000; "
}
url=url+"leftTicketDTO.train_date="+r"2019-11-24"+"&leftTicketDTO.from_station="+"XAY"+"&leftTicketDTO.to_station="+r"HZH"+"&purpose_codes=ADULT"
#print(url) 已经检查过生成的URL是正确的
#request请求获取主页
r = requests.get(url,headers=headers,verify=False)
r.raise_for_status() #如果发送了一个错误的请求,会抛出异常
r.encoding = r.apparent_encoding
result=((((r.content).split('['))[1]).split(']'))[0]#处理返回数据
re_list= result.split(",")
for x in re_list:
ticket_info=x.split('|')
CheCi=ticket_info[3]
station_start=CheCi_(ticket_info[4])
station_stop=CheCi_(ticket_info[7])
start_time=ticket_info[8]
stop_time=ticket_info[9]
time_fucked_up =ticket_info[10]
if(len(ticket_info[31])==0):
first_class_seat="无"
else:
first_class_seat=ticket_info[31]
if(len(ticket_info[30])==0):
second_class_seat="无"
else:
second_class_seat=ticket_info[30]
if(len(ticket_info[23])==0):
soft_sleep="无"
else:
soft_sleep=ticket_info[23]
if(len(ticket_info[28])==0):
hard_sleep="无"
else:
hard_sleep=ticket_info[28]
if(len(ticket_info[29])==0):
hard_seat="无"
else:
hard_seat=ticket_info[29]
if(len(ticket_info[26])==0):
no_seat="无"
else:
no_seat=ticket_info[26]
print '出发站:'+station_start+'\n'+"终点站:"+station_stop+'\n'+"出发时间:"+start_time+'\n'+"到达时间:"+stop_time+'\n'+"耗时:"+time_fucked_up+'\n'+"一等座:"+first_class_seat+'\n'+"二等座:"+second_class_seat+'\n'+"软卧:"+soft_sleep+'\n'+"硬卧:"+hard_sleep+'\n'+"硬座:"+hard_seat+'\n'+"无座:"+no_seat+'\n'+'\n\n'
main()