#!/usr/bin/env python
# -*- coding:utf8 -*-
'''
这是关于车站接口的定义
'''
import requests
import sys
import re
from requests.packages.urllib3.exceptions import InsecureRequestWarning
# 禁用安全请求警告
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
def get_stations(url):
try:
stationhtml = requests.get(url,verify=False,timeout=3)
stationtxt = stationhtml.text
except:
print('网络连接不稳定,请稍后再试!')
sys.exit()
list1 = stationtxt.split('@')
ll = len(list1)
stations = {}
for i in range(1,ll):
st = list1[i]
list2 = st.split('|')
code = list2[2]
pinyin = list2[3]
hanzi = list2[1]
jianpin = list2[0]
stations[code] = [pinyin,hanzi,jianpin]
return stations
def prase_station(s,stations):
for k in stations.keys():
if s in stations[k]:
return k
def station_tofile(stations,file):
with open(file,'w') as f:
for st in stations.items():
f.write(str(st))
f.write('\n')
def station_valid(s,stations):
valid = False
vl = stations.values()
for t in vl:
if s in t:
valid = True
break
return valid
url_station = 'https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.9037'
stations = get_stations(url_station)
url_left = 'https://kyfw.12306.cn/otn/leftTicket/init'
leftTicketHtml = requests.get(url_left,verify=False)
url_leftTicket = re.findall(r'leftTicket/query\w?',leftTicketHtml.text)
if __name__ == '__main__':
print(__name__)
print(__doc__)
print(__file__)
print(re.__name__)
print(re.__doc__)
print(re.__file__)
python火车余票查询(stationapi)
最新推荐文章于 2021-08-19 09:25:24 发布