python_爬虫爬取高德地图公交线路_赠高德地图key

python_爬取高德地图某条公交线路_赠高德地图key

原创不易,转发请带本文链接

使用高德地图可以搜索单条公交线路,使用代码可以一次性爬取高德地图公交
赠送一个企业高德key,大部分高德的接口日访问量30万
88d2e276d696bb7d9cbbc748bd11a405

import json
import pandas as pd
import requests
from utils.geolocataion_converting import gcj02_to_wgs84
from utils.read_write import writeOneCSV, writeCSV

'''
    此文件用于使用公交线路号爬取指定公交线路数据
'''

header = {
    'Accept': '*/*',
    'Accept-Language': 'zh-CN,zh;q=0.8',
    'Connection': 'keep-alive',
    'Host': 'restapi.amap.com',
    'Origin': 'https://restapi.amap.com',
    'Referer': 'https://restapi.amap.com/',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36 SE 2.X MetaSr 1.0',
}

# 开始请求高德地图的API
def get_geo(linename):
    url = "https://restapi.amap.com/v3/bus/linename?s=rsv3&extensions=all&key=88d2e276d696bb7d9cbbc748bd11a405&output=json&pageIndex=1&" \
          "city=%E6%B7%B1%E5%9C%B3&offset=1&keywords=" + str(
        linename) + "路" + "&platform=JS&logversion=2.0&appname=file%3A%2F%2F%2FD%3A%2F%25E9%25A1%25B9%25E7%259B%25AE%25E6%2596%2587%25E4%25BB%25B6%2F%25E5%25BB%25BA%25E8%25A7%2584%25E9%2599%25A2%2F%25E5%2589%258D%25E7%25AB%25AF%25E8%258E%25B7%25E5%258F%2596%25E5%2585%25AC%25E4%25BA%25A4%25E8%25B7%25AF%25E7%25BA%25BF%25E6%2595%25B0%25E6%258D%25AE%2Fbus_route.html&csid=D177451A-E355-4D49-A2CB-B87937FD9208&sdkversion=1.4.15"
    try:
        # 添加header
        response = requests.get(url, headers=header)
        data = json.loads(response.text)
        return data
    except ValueError:
        print("出现异常,请重新运行!" + str(ValueError))


#把数据处理成arcgis能够处理的线
def write_polyline(LID, polyline, path):
    lon_lat_list = polyline.split(';')
    geo_list = []
    for index in range(len(lon_lat_list)):
        geo = []
        lonlat = lon_lat_list[index].split(',')
        lon = lonlat[0]
        lat = lonlat[1]
        geo.append(LID)
        geo.append(index)
        geo.append(lon)
        geo.append(lat)
        geo_list.append(geo)
    writeCSV(geo_list, path)


def write_polyline_wgs84(LID, polyline, path):
    lon_lat_list = polyline.split(';')
    geo_list = []
    for index in range(len(lon_lat_list)):
        geo = []
        lonlat = lon_lat_list[index].split(',')
        lon = lonlat[0]
        lat = lonlat[1]
        lon = float(lon)
        lat = float(lat)
        lng, lat = gcj02_to_wgs84(lon, lat)
        geo.append(LID)
        geo.append(index)
        geo.append(lng)
        geo.append(lat)
        geo_list.append(geo)
    writeCSV(geo_list, path)


def write_bus_stops(LID,all_bus_name, busstops, path):
    bus_station_list = []
    for station in busstops:
        bus_station = []
        name = station['name']
        location = station['location']
        id = station['id']
        sequence = station['sequence']
        lonlat = location.split(',')
        lon = lonlat[0]
        lat = lonlat[1]
        bus_station.append(LID)
        bus_station.append(all_bus_name)
        bus_station.append(name)
        bus_station.append(id)
        bus_station.append(sequence)
        bus_station.append(lon)
        bus_station.append(lat)
        bus_station_list.append(bus_station)
    writeCSV(bus_station_list, path)


def write_bus_stops_wgs84(LID,all_bus_name, busstops, path):
    bus_station_list = []
    for station in busstops:
        bus_station = []
        name = station['name']
        location = station['location']
        sequence = station['sequence']
        id = station['id']
        lonlat = location.split(',')
        lon = lonlat[0]
        lat = lonlat[1]
        lon = float(lon)
        lat = float(lat)
        lng, lat = gcj02_to_wgs84(lon, lat)
        bus_station.append(LID)
        bus_station.append(all_bus_name)
        bus_station.append(name)
        bus_station.append(id)
        bus_station.append(sequence)
        bus_station.append(lng)
        bus_station.append(lat)
        bus_station_list.append(bus_station)
    writeCSV(bus_station_list, path)


if __name__ == "__main__":
    # 序号	业户名称	 线路编号	起点站	终点站	 上行途径站点	 下行途径站点
    src = 'D:\data\jianguiyaun\\2019年9月深圳公交数据.xlsx'
    geo_path = 'D:\data\jianguiyaun\\深圳单向公交线路数据\\'
    route = pd.read_excel(src)
    route.drop(0)
    for row in route.itertuples(index=True, name='Pandas'):
        LID = getattr(row, '序号')
        bus_name = getattr(row, '线路编号')
        json_data = get_geo(bus_name)
        bus = []
        if int(json_data['count']) > 0:
            id = json_data['buslines'][0]['id']
            start_stop = json_data['buslines'][0]['start_stop']
            end_stop = json_data['buslines'][0]['end_stop']
            company = json_data['buslines'][0]['company']
            basic_price = json_data['buslines'][0]['basic_price']
            citycode = json_data['buslines'][0]['citycode']
            distance = json_data['buslines'][0]['distance']
            bus.append(LID)
            bus.append(id)
            bus.append(bus_name)
            bus.append(start_stop)
            bus.append(end_stop)
            bus.append(citycode)
            bus.append(distance)
            bus.append(company)
            bus.append(basic_price)
            writeOneCSV(bus,geo_path+'geo_bus.csv')
            polyline = json_data['buslines'][0]['polyline']
            write_polyline(LID,polyline,geo_path+'geo_bus_line_GCJ02.csv')
            write_polyline_wgs84(LID, polyline, geo_path + 'geo_bus_line_WGS84.csv')

            busstops = json_data['buslines'][0]['busstops']
            write_bus_stops(LID, bus_name,busstops, geo_path + 'geo_bus_station_GCJ02.csv')
            write_bus_stops_wgs84(LID, bus_name,busstops, geo_path + 'geo_bus_station_WGS84.csv')

geolocataion_converting.py
2019深圳公交线路数据
read_write.py
如需帮忙抓取某条公交车线路数据,请私聊我,我这暂时只有深圳的公交线路数据

你可能感兴趣的:(python爬虫,python,url,json,gis,大数据)