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 | 输出文件类型 | 字符串 |