这篇文章给大家分享的是有关Python如何爬取高德地图地铁线路及站点数据的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
前言
本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。
# coding=utf-8
import requests
import time
import json
import ast
import os
import utils
from lxml import etree
PAGE_URL = 'http://map.amap.com/subway/index.html?&1100'
DATA_URL = 'http://map.amap.com/service/subway?srhdata='
HEADER = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"}
def fetchAllCity(url, header):
r = requests.get(url, header)
html = r.content
element = etree.HTML(html)
options = element.xpath("//a[contains(@class, 'city')]")
cities = []
for option in options:
city = {
'id': option.get('id'),
'name': option.get('cityname'),
'text': option.text
}
cities.append(city)
return cities
def parseCityData(citys):
lw = open('./lwkt.txt', 'w')
lw.write('wkt' + '\n')
pw = open('./pwkt.txt', 'w')
pw.write('wkt' + '\n')
for city in citys:
parseCityPointFromApi(city, lw, pw)
def parseCityPointFromApi(city, lw, pw):
url = DATA_URL + "{}_drw_{}.json".format(city['id'], city['name'])
print(url)
json_str = requests.get(url).text
res = json.loads(json_str)
res = res['l']
for r in res:
st = r['st']
coords = []
for s in st:
_coords = s.get('sl', '').split(',')
coords.append(_coords)
pwkt = '"POINT ({} {})"'.format(_coords[0], _coords[1])
pw.write(pwkt + '\n')
new_coords = ','.join(['%s %s' % (coord[0], coord[1]) for coord in coords])
lwkt = '"LINESTRING(%s)"' % new_coords
lw.write(lwkt + '\n')
def main():
cities = fetchAllCity(PAGE_URL, HEADER)
print(cities)
parseCityData(cities)
if __name__ == '__main__':
main()
感谢各位的阅读!关于“Python如何爬取高德地图地铁线路及站点数据”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/4848094/blog/4745869