网络编程
位置:首页>> 网络编程>> Python编程>> Python爬虫爬取属于自己的地铁线路图

Python爬虫爬取属于自己的地铁线路图

作者:编程简单学  发布时间:2021-09-10 11:46:23 

标签:Python,爬虫,爬取,地铁线路图

前言:

网上找的地铁线路图大多数都不太清晰,而且有水印,对本人这种视力不好的人来说看起来是真的不方便。所以能不能制作属于自己的地铁线路图呢?好不好看无所谓,主要是高清 * ,要看清楚各个站点!想了想,主要还是缺乏站点数据,有数据了图自然就有了。经过网上查询,发现高德地图上有专门的地铁线路图,但是不能导出数据或图片,只好自己想办法抓取了。下面以西安地铁线路图为例介绍方法。

一.高德地图数据爬取

1.爬取思路

首先,谷歌浏览器打开 高德地图官网 ,点击上方菜单栏 地铁 进入地铁线路网站如下,网址:http://map.amap.com/subway/index.html。

Python爬虫爬取属于自己的地铁线路图

按 F12 或右击 检查 进入调试页面,点击 Network 选项。在网页上先点击 西安 ,可以发现箭头2出新增两行响应信息,鼠标左击可以发现箭头3处出现真实的请求地址等信息。

Python爬虫爬取属于自己的地铁线路图

复制请求URL地址(http://map.amap.com/service/subway?_1612234237437&srhdata=6101_drw_xian.json),在浏览器新页面打开可以看见返回的是 json 数据,里面包含了各线路站点信息,正是我们想要的。

Python爬虫爬取属于自己的地铁线路图

可以复制json数据在json在线验证网站上进行分析,以便于后续解析数据(网址:http://www.json.cn/#)。如下图所示,显示的是1号线沣河森林公园站的相关信息:中文名称、经纬度(应该是火星坐标系?)、拼音名称等。

Python爬虫爬取属于自己的地铁线路图

我们通过python爬虫爬取各线路各站点的 名称、经纬度 信息,导出到文本文件,以供后续使用。

2.python核心代码

获取网页内容:


def getHtml(url):
   user_agent = random.choice(USER_AGENTS)
   headers = {
       "Host":"map.amap.com",
       'User-Agent': user_agent
   }
   try:
       response = requests.get(url, headers=headers)
       #print(response.url)
       text = response.content
       return text
   except:
       print("爬取失败!")

解析json数据:


def parse_page(text):
   lines_list = json.loads(text).get('l')
   # 地铁线路信息表
   lineInfo_list = []
   for line in lines_list:
       #每条线的信息集合
       lineInfo = {}
       lineInfo['ln'] = line.get('ln')
       print(lineInfo['ln'])

#线路站点列表
       station_list = []
       st_list = line.get('st')
       for st in st_list:
           station_dict = {}
           station_dict['name'] = st.get('n')
           coord = st.get('sl')
           station_dict['lat'] = coord.split(',')[0]
           station_dict['lon'] = coord.split(',')[-1]
           print("站名称:", station_dict['name'])
           print("经度:", station_dict['lat'])
           print("纬度:", station_dict['lon'])
           station_list.append(station_dict)
           #pass
       print('-----------------------------------')
       lineInfo['st'] = station_list
       lineInfo['kn'] = line.get('kn')
       lineInfo['ls'] = line.get('ls')
       lineInfo['cl'] = line.get('cl')
       lineInfo_list.append(lineInfo)
   #返回各线路信息列表
   return lineInfo_list

保存站点数据(站名称、经纬度):


def save_file(filename, lineInfo):
   #print("开始写入文件......")
   with open(filename, 'a', encoding='utf-8') as f:
       for st in lineInfo['st']:
           f.write(st['name'] + "  " + st['lat'] + "  " + st['lon'] + "\n")
   #print("写入文件完成!")

爬取完成后,生成的数据如下:

Python爬虫爬取属于自己的地铁线路图

二.生成shp文件并导出图片

主要思路:调用Arcpy函数生成shp文件-——>点转线——>设置符号样式——>导出图片。

1.文本点生成shp代码


def create_shp(text,dirpath):
   point_shpname = text.split('.')[0] + "_point.shp"
   line_shpname = text.split('.')[0] + "_line.shp"
   f = open(text, 'r')
   lines = f.readlines()
   spatRef = arcpy.SpatialReference(4326)
   createFC = arcpy.CreateFeatureclass_management(dirpath, point_shpname, "POINT", "", "", "",spatRef)
   arcpy.AddField_management(createFC, "name", "TEXT")
   arcpy.AddField_management(createFC, "lat", "DOUBLE")
   arcpy.AddField_management(createFC, "lon", "DOUBLE")
   cur = arcpy.InsertCursor(createFC)

for line in lines:
       info = line.strip().split("  ")
       row = cur.newRow()
       name = info[0]
       point = arcpy.Point()
       point.X = float(info[1])
       point.Y = float(info[2])
       pointGeometry = arcpy.PointGeometry(point)
       row.shape = pointGeometry
       row.name = name
       row.lon = point.X
       row.lat = point.Y
       cur.insertRow(row)

#站点生成线
   arcpy.PointsToLine_management(point_shpname, line_shpname)

2.Arcmap设置样式

将生成的点shp与线shp矢量文件加载到arcmap当中设置样式与符号大小,然后导出地图为图片。记得导出地图时图片分辨率选择为300dpi。

Python爬虫爬取属于自己的地铁线路图

最终,如下图所示属于自己的地铁线路图就制作完成了。图片估计上传到微信上就不是原图了,又会变模糊,但是实际看起来还是比较清楚的。

Python爬虫爬取属于自己的地铁线路图

来源:https://blog.csdn.net/weixin_54556126/article/details/121978670?utm_source=tuicool&utm_medium=referral

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com