获取高德POI接口数据(自用)

1 篇文章 0 订阅
1 篇文章 0 订阅

高德webAPI开发文档入口
高德开放平台控制台入口

poi数据获取代码 V_0.1.0

import requests
import json
import pandas as pd
import time

def amap_poi(key,keywords,city,page,excel_name):
    df_new = pd.DataFrame()
    for i in range(1,page+1):
        response = requests.get("http://restapi.amap.com/v3/place/text?key="+str(key)+"&keywords="+str(keywords)+"&types=&city="+str(city)+"&children=1&offset=50&page="+str(i)+"&extensions=all")
        df = pd.DataFrame(data=json.loads(response.text)['pois'])
        df = df[['name','id','location','type','typecode','pname','cityname','adname','address','pcode','adcode','citycode']]
        df_new = pd.concat([df,df_new],axis=0)
        time.sleep(0.5)
    df_new = df_new.drop_duplicates(subset=['id'], keep='first')
    df_new = df_new[['name','id','location','type','typecode','pname','cityname','adname','address','pcode','adcode','citycode']].reset_index()
    df_new.to_excel(str(excel_name)+'.xlsx')
    print("已完成,表名:"+str(excel_name)+".xlsx")

函数调用

amap_poi(key,keywords,city,page,excel_name)

调用示例
amap_poi('5571e21358d4a1e24 ', ‘xx县’, ‘xx市’, 18, ‘xx县POI数据’)
示例中的key为无效key

调用参数说明

参数名说明数据类型
key密钥字符串
keywords搜索关键字字符串
city所属城市字符串
page页数整型
excel_name输出表格名称字符串

更新版本 V_0.2.0

新增输出格式选择功能(目前支持的格式有xlsx、shp)
取消页数输入,默认输出全量数据

import requests
import json
from pandas import DataFrame
import time
from pandas import concat
import geopandas



def amap_poi(key,keywords,city,out_format):
    tar = 1
    page = 0
    df_new = DataFrame()
    while tar != 0:
        page = page + 1
        response = requests.get("http://restapi.amap.com/v3/place/text?key="+str(key)+"&keywords="+str(keywords)+"&types=&city="+str(city)+"&children=1&offset=50&page="+str(page)+"&extensions=all")
        tar = int(json.loads(response.text)['count'])
        if tar != 0:
            df = DataFrame(data=json.loads(response.text)['pois'])
            df = df[['name','id','location','type','typecode','pname','cityname','adname','address','pcode','adcode','citycode']]
            df_new = concat([df,df_new],axis=0)
            time.sleep(0.1)
    if len(df_new) != 0:
        df_new = df_new.drop_duplicates(subset=['id'], keep='first')
        df_new = df_new[['name','id','location','type','typecode','pname','cityname','adname','address','pcode','adcode','citycode']].reset_index(drop=True)
        if out_format == 'xlsx':
            df_new.to_excel(str(city)+str(keywords)+'POI数据.xlsx')
            print("成功,结果已保存" + "表名:" + str(city) + str(keywords) + "POI数据.xlsx," + "默认保存在exe文件同级目录")
        elif out_format == 'shp':
            df_new['location'] = df_new['location'].apply(lambda x: "POINT ("+x.split(',')[0]+" "+x.split(',')[1]+")")
            df_new = df_new[['name','id','location','type','typecode','pname','cityname','adname','address','pcode','adcode','citycode']].astype("str")
            gs = geopandas.GeoSeries.from_wkt(df_new['location'])
            geo_df = geopandas.GeoDataFrame(df_new, geometry=gs)
            geo_df.to_file(str(city)+str(keywords)+'POI数据', driver='ESRI Shapefile', encoding='utf-8')
            print("成功,结果已保存,默认保存在exe文件同级目录")
        else:
            pass
        time.sleep(5)
    else:
        print("失败,暂无查询结果")
        time.sleep(3)
    return 0


key = input("请输入key:")
keywords = input("请输入查询关键字:")
city = input("请输入查询城市:")
out_format = input("请输入需要输出的格式,支持xlsx、shp:")
print("请等待,程序正在运行")

函数调用

amap_poi(key,keywords,city,out_format)

调用参数说明

参数名说明数据类型
key密钥字符串
keywords搜索关键字字符串
city所属城市字符串
out_format输出文件类型字符串
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值