-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetro_crawler.py
224 lines (193 loc) · 6.53 KB
/
metro_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/local/env python
"""
======================
Metro stations crawler
======================
Data source:
---------------------
* Beijing metro: https://map.bjsubway.com/
* Shanghai metro: http://m.shmetro.com/core/shmetro/mdstationinfoback_new.ashx?act=getAllStations
* Guangzhou metro http://cs.gzmtr.com/base/doLoadLines.do?callback=gzmetro
* Shenzhen metro http://www.szmc.net/ver2/operating/search?scode=0101&xl=1
Output format Example:
---------------------
{
'city': 'BJ',
'lines': [
{
'line_name': '1号线',
'stations': [
{'station': '苹果园', 'x': },
{'station': '古城'}
]
},
{
'line_name': '2号线',
'stations': [
{'station': '西直门'},
{'station': '积水潭'}
]
}
]
}
Usage Example
$ python metro_crawler.py BJ bj.metro.json
"""
import re
import sys
import json
import logging
import argparse
import requests
from bs4 import BeautifulSoup
from xml.etree import ElementTree
FORMAT = '[%(levelname)s]: %(message)s'
logging.basicConfig(format=FORMAT)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def save_file(outpath, data):
# save the result
with open(outpath, 'w') as fp:
json.dump(data, fp)
logger.info('Crawl finished. Output file: {}'.format(outpath))
def crawl_sz(outpath):
url = 'http://www.szmc.net/public/scripts/sites.js'
res = requests.get(url)
lines = re.search(r'(?<=var xls=)\S+(?=;)', res.text).group()
lines = re.findall(r'\'(\w+)\'', lines)
metros = {
'city': 'SZ',
'lines': []
}
for idx, name in enumerate(lines):
pattern = r'(?<=var site%d=)\S+(?=;)' % (idx + 1)
data = re.search(pattern, res.text).group()
stations = re.findall(r'(?<=n:\')\w+', data)
stations = list(map(lambda x: {'station_name': x}, stations))
line = {'line_name': name, 'stations': stations}
metros['lines'].append(line)
save_file(outpath, metros)
def crawl_gz(outpath):
prefix = 'gzmetro'
url = 'http://cs.gzmtr.com/base/doLoadLines.do?callback={}'.format(prefix)
res = requests.get(url)
metros = {
'city': 'GZ',
'lines': []
}
if res.status_code == 200:
try:
data = res.text.lstrip(prefix) # exclude callback
data = data[1:len(data) - 1] # exclude parenthesis
lines = json.loads(data)
for node in lines['lines']:
line = {'line_name': node['lineName']}
line['stations'] = list(map(lambda x: {'station_name': x['stageName']}, node['stages']))
metros['lines'].append(line)
# save the result
save_file(outpath, metros)
except Exception:
logger.exception('Failed to parse the metro station output')
else:
logger.error('Failed to get guangzhou metro stations, code: {}'.format(
res.status_code
))
def crawl_sh(outpath):
# step 1 - get all lines
def parse_line(line):
link = line.select_one('a')
line_name = link.text.strip()
line_no = int(re.search(r'(?<=/axlcz)\d+', link.attrs['href']).group())
return (line_no, line_name)
url = 'http://service.shmetro.com/axlcz01/index.htm'
res = requests.get(url)
line_names = []
if res.status_code == 200:
bs = BeautifulSoup(res.text, features='html.parser')
lines_nodes = bs.select('ul.site_select_list > li')
line_names = dict(map(lambda x: parse_line(x), lines_nodes))
else:
logger.error('Failed to get shanghai metro lines')
# step 2 - get all stations
url = 'http://m.shmetro.com/core/shmetro/mdstationinfoback_new.ashx?act=getAllStations'
res = requests.get(url)
metros = {
'city': 'SH',
'lines': []
}
if res.status_code == 200:
try:
stations = json.loads(res.text)
prev = ''
for station in stations:
line = int(station['key'][:2])
line_name = line_names[line]
if prev != line_name:
metros['lines'].append({
'line_name': line_name,
'stations': []
})
metros['lines'][-1]['stations'].append({
'station_name': station['value']
})
prev = line_name
# save the result
save_file(outpath, metros)
except Exception:
logger.exception('Failed to parse the metro station output')
else:
logger.error('Failed to get shanghai metro stations, code: {}'.format(
res.status_code
))
def crawl_bj(outpath):
url = 'https://map.bjsubway.com/subwaymap/beijing.xml'
res = requests.get(url)
metros = {
'city': 'BJ',
'lines': []
}
if res.status_code == 200:
try:
res.encoding = 'utf-8'
root = ElementTree.fromstring(res.text) # root = <sw>
for line_child in root:
# <l> tag
line = {'line_name': line_child.attrib.get('lb')}
stations = line_child.findall('./p')
stations = filter(lambda x: x.attrib.get('lb', '') != '', stations)
line['stations'] = list(map(lambda x: {'station_name': x.attrib.get('lb')}, stations))
if line_child.attrib.get('loop') == 'true':
line['stations'].append(line['stations'][0])
metros['lines'].append(line)
# save the result
save_file(outpath, metros)
except Exception:
logger.exception('Failed to parse the metro station xml output')
else:
logger.error('Failed to get Beijing metro stations, code: {}'.format(
res.status_code
))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'city',
type=str,
help='The city you need to grap, current support BJ, SH, GZ, SZ'
)
parser.add_argument(
'outpath',
type=str,
help='The output path for metro stations map'
)
args = parser.parse_args(sys.argv[1:])
_CRWAL_MAPPING = {
'BJ': crawl_bj,
'SH': crawl_sh,
'GZ': crawl_gz,
'SZ': crawl_sz,
}
crawler = _CRWAL_MAPPING.get(args.city, None)
if crawler is not None:
crawler(args.outpath)
else:
logger.info('{} city is not supported'.format(args.city))