-
Notifications
You must be signed in to change notification settings - Fork 0
/
12306_crawler.py
62 lines (53 loc) · 1.67 KB
/
12306_crawler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#! /usr/bin/python
# -*- coding:utf-8 -*-
#author:zhanhao
#Date:2016.12.11
#file:12306_crawler.py
import urllib.request
import json
from data import keys,results,station_names_url,purpose_codes
import re
def set_station_names(station):
"""
将输入的中文转化成query_url需要的英文缩写形式,查询不到会弹出IndexError。
"""
stations = urllib.request.urlopen(station_names_url).read().decode("utf-8")
station = re.findall(station+"\|(.*?)\|",stations)
station = station[0]
return(station)
def print_data(start,end,date,purpose_code):
"""
提交查询,打印结果
"""
query = "https://kyfw.12306.cn/otn/lcxxcx/query?purpose_codes="+purpose_codes[purpose_code]+\
"&queryDate="+date+"&from_station="+start+"&to_station="+end
html = urllib.request.urlopen(query)
trains = json.loads(html.read().decode("utf-8"))["data"]["datas"]
for train in trains:
for key in keys:
print(results[key]+":"+train[key],end="\t")
print("\n")
def start_query():
"""
初始化查询参数
"""
while(True):
try:
start = set_station_names(input("出发站:"))
break
except IndexError:
print("没有此站信息")
while(True):
try:
end = set_station_names(input("到达站:"))
break
except IndexError:
print("没有此站信息")
date = input("日期(1970-01-01):")
purpose_code = input("成人/学生票:")
try:
print_data(start,end,date,purpose_code)
except KeyError:
print("没有查询到车次信息!")
if __name__ == "__main__":
start_query()