当前位置: 华文星空 > 新闻

从地图软件中提取城市的地铁线路数据

2024-02-07新闻

获取高德地图数据

第一步,打开高德地图:https:// map.amap.com/subway/ind ex.html

第二步,打开开发者工具(按 F12 或 ⌥ ⌘ I )。注意,如果你是第一次打开,浏览器可能会阻止你,请允许启动开发者权限。

第三步,切换到 Network 标签页。第一次打开时,这里可能是空的。

或者也可能发现已经堆满了非常多内容,你可以点击清空按钮。方便等会找到想要的数据。

第四步,刷新页面,会发现现在已经多了好多东西了。其中我划红线看到的 subway.json ,就是我们要找的数据。

不过,目前默认是北京的数据。我想收集广州的。所以,回到网页中,切换城市。

然后发现,这里多了2个文件。第一个的 drw_guangzhou.json 就是我要的。

第五步,把它保存到桌面,作为 subway.json

数据分析:

"l" 是一个数组,包含了所有线路的数据,也就是网页中这个黑色导航栏。

遍历数组 "l" 中的每个元素:提取 "ln" 线路名称,"st" 也是一个数组,里面是所有站点的数据。然后再遍历数组 "st" 中的每个元素,提取里面的 "n" 站点名称。

OK。有了思路,就可以开始写代码了。

整理地铁线路数据

新建一个 python 文件,也放在桌面,运行它

import json # 地图数据 data_file = 'subway.json' # 导出的文件 output_file = 'output.json' # 导入数据 json_data = {} with open ( data_file , 'r' , encoding = "utf-8" ) as file : json_data = json . load ( file ) # 整理数据 lines = [] for item in json_data [ 'l' ]: line_name = item [ 'ln' ] station_names = [ station [ 'n' ] for station in item [ 'st' ]] lines . append ({ line_name : station_names }) # 保存成文件 with open ( output_file , 'w' , encoding = "utf-8" ) as file : json . dump ( lines , file , ensure_ascii = False , indent = 4 )

就得到了这样一份数据

[ { "1号线" : [ "西塱" , "坑口" , "花地湾" , "芳村" , "黄沙" , "长寿路" , "陈家祠" , "西门口" , "公园前" , "农讲所" , "烈士陵园" , "东山口" , "杨箕" , "体育西路" , "体育中心" , "广州东站" ] }, { "佛山2号线" : [ "广州南站" , "林岳东(2号线)" , "林岳西" , "石洲" , "仙涌" , "花卉世界" , "登洲" , "湾华" , "石梁" , "魁奇路" , "沙岗" , "石湾" , "张槎" , "智慧新城" , "绿岛湖" , "湖涌" , "南庄" ] }, { "2号线" : [ "广州南站" , "石壁" , "会江" , "南浦" , "洛溪" , "南洲" , "东晓南" , "江泰路" , "昌岗" , "江南西" , "市二宫" , "海珠广场" , "公园前" , "纪念堂" , "越秀公园" , "广州火车站" , "三元里" , "飞翔公园" , "白云公园" , "白云文化广场" , "萧岗" , "江夏" , "黄边" , "嘉禾望岗" ] }, { "3号线" : [ "番禺广场" , "市桥" , "汉溪长隆" , "大石" , "厦滘" , "沥滘" , "大塘" , "客村" , "广州塔" , "珠江新城" , "体育西路" , "石牌桥" , "岗顶" , "华师" , "五山" , "天河客运站" ] }, { "3号线(北延段)" : [ "机场北(2号航站楼)" , "机场南(1号航站楼)" , "高增" , "人和" , "龙归" , "嘉禾望岗" , "白云大道北" , "永泰" , "同和" , "京溪南方医院" , "梅花园" , "燕塘" , "广州东站" , "林和西" , "体育西路" ] }, { "4号线" : [ "黄村" , "车陂" , "车陂南" , "万胜围" , "官洲" , "大学城北" , "大学城南" , "新造" , "石碁" , "海傍" , "低涌" , "东涌" , "庆盛" , "黄阁汽车城" , "黄阁" , "蕉门" , "金洲" , "飞沙角" , "广隆" , "大涌" , "塘坑" , "南横" , "南沙客运港" ] }, { "5号线" : [ "滘口" , "坦尾" , "中山八" , "西场" , "西村" , "广州火车站" , "小北" , "淘金" , "区庄" , "动物园" , "杨箕" , "五羊邨" , "珠江新城" , "猎德" , "潭村" , "员村" , "科韵路" , "车陂南" , "东圃" , "三溪" , "鱼珠" , "大沙地" , "大沙东" , "文冲" , "双沙" , "庙头" , "夏园" , "保盈大道" , "夏港" , "黄埔新港" ] }, { "6号线" : [ "香雪" , "萝岗" , "苏元" , "暹岗" , "金峰" , "黄陂" , "高塘石" , "柯木塱" , "龙洞" , "植物园" , "长湴" , "天河客运站" , "燕塘" , "天平架" , "沙河顶" , "黄花岗" , "区庄" , "东山口" , "东湖" , "团一大广场" , "北京路" , "海珠广场" , "一德路" , "文化公园" , "黄沙" , "如意坊" , "坦尾" , "河沙" , "沙贝" , "横沙" , "浔峰岗" ] }, { "7号线" : [ "燕山" , "水西" , "萝岗" , "科丰路" , "加庄" , "姬堂" , "大沙东" , "裕丰围" , "长洲" , "深井" , "大学城南" , "板桥" , "员岗" , "南村万博" , "汉溪长隆" , "钟村" , "谢村" , "石壁" , "广州南站" , "大洲" , "陈村北" , "陈村" , "锦龙" , "南涌" , "美的" , "北滘公园" , "美的大道" ] }, { "8号线" : [ "滘心" , "亭岗" , "石井" , "小坪" , "石潭" , "聚龙" , "上步" , "同德" , "鹅掌坦" , "西村" , "彩虹桥" , "陈家祠" , "华林寺" , "文化公园" , "同福西" , "凤凰新村" , "沙园" , "宝岗大道" , "昌岗" , "晓港" , "中大" , "鹭江" , "客村" , "赤岗" , "磨碟沙" , "新港东" , "琶洲" , "万胜围" ] }, { "9号线" : [ "高增" , "清塘" , "清布" , "莲塘" , "马鞍山公园" , "花都广场" , "花果山公园" , "花城路" , "广州北站" , "花都汽车城" , "飞鹅岭" ] }, { "13号线" : [ "鱼珠" , "裕丰围" , "双岗" , "南海神庙" , "夏园" , "南岗" , "沙村" , "白江" , "新塘" , "官湖" , "新沙" ] }, { "14号线" : [ "嘉禾望岗" , "白云东平" , "夏良" , "太和" , "竹料" , "钟落潭" , "马沥" , "新和" , "太平" , "神岗" , "赤草" , "从化客运站" , "东风" ] }, { "14号线支线(知识城线)" : [ "新和" , "红卫" , "新南" , "枫下" , "知识城" , "何棠下" , "旺村" , "汤村" , "镇龙北" , "镇龙" ] }, { "18号线" : [ "冼村" , "磨碟沙" , "龙潭" , "沙溪" , "南村万博" , "番禺广场" , "横沥" , "万顷沙" ] }, { "21号线" : [ "员村" , "天河公园" , "棠东" , "黄村" , "大观南路" , "天河智慧城" , "神舟路" , "科学城" , "苏元" , "水西" , "长平" , "金坑" , "镇龙西" , "镇龙" , "中新" , "坑贝" , "凤岗" , "朱村" , "山田" , "钟岗" , "增城广场" ] }, { "22号线" : [ "陈头岗" , "广州南站" , "市广路" , "番禺广场" ] }, { "APM线" : [ "广州塔" , "海心沙" , "大剧院" , "花城大道" , "妇儿中心" , "黄埔大道" , "天河南" , "体育中心南" , "林和西" ] }, { "广佛线" : [ "沥滘" , "南洲" , "石溪" , "燕岗" , "沙园" , "沙涌" , "鹤洞" , "西塱" , "菊树" , "龙溪" , "金融高新区" , "千灯湖" , "礌岗" , "南桂路" , "桂城" , "朝安" , "普君北路" , "祖庙" , "同济路" , "季华园" , "魁奇路" , "澜石" , "世纪莲" , "东平" , "新城东" ] } ]

继续扩展一下。

我想找出某个站点是否和其他站点有交集,也就是中转站

筛选包含站点的线路

import json # 前面整理好的数据 data_file = 'output.json' # 要寻找交集的站点名称 find_stations = '广州东站' # 导入文件 with open(data_file, 'r', encoding="utf-8") as file: data = json.load(file) # 搜索符合的站点信息 lines = [line for line in data if any(find_stations in stations for stations in line.values())] # 输出 print(json.dumps(lines, indent=2, ensure_ascii=False))

导出:

[ { "1号线" : [ "西塱" , "坑口" , "花地湾" , "芳村" , "黄沙" , "长寿路" , "陈家祠" , "西门口" , "公园前" , "农讲所" , "烈士陵园" , "东山口" , "杨箕" , "体育西路" , "体育中心" , "广州东站" ] }, { "3号线(北延段)" : [ "机场北(2号航站楼)" , "机场南(1号航站楼)" , "高增" , "人和" , "龙归" , "嘉禾望岗" , "白云大道北" , "永泰" , "同和" , "京溪南方医院" , "梅花园" , "燕塘" , "广州东站" , "林和西" , "体育西路" ] } ]

计算站点之间距离