高德上面有个poi检索,相信很多人都非常感兴趣。
奈何他没有给出示例代码,不想华为云的语音转换各种示例代码很完整,我自己写一个作为参考拿来看看
# 本程序可以完成的任务是:输入一个多边形区域可以检索该区域内的所有店面并保存下来
import json,time,requests
#定义获取poi函数
# 函数get_poi 通过输入变量polygon来获取指定多边形以内的全部店面信息,输入值:polygon 输出值:poi组成的out列表
def get_poi(polygon):
url_base = 'https://restapi.amap.com/v5/place/polygon?key={0}&polygon={1}&types=061100&show_fields=business&page_size=25&page_num={2}'
key='***填写自己申请的key***'
page_num = 1
f = ['name', 'id' ,'type', 'cityname', 'address', 'business']
#-----------以下开始核心功能-----------
#开始数据获取并写入到out中
while page_num <=100:
url = url_base.format(key,polygon,page_num)
response = requests.get(url)
#获取信息上线每天不能高于3万次
data = json.loads(response.text)
pois_count = int(data['count'])
p = data['pois']
for poi in p:
line = []
for i in f:
if 'tel' not in poi['business'].keys():
continue
elif i != 'business':
line.append(poi[i])
else:
line.append(poi['business']['tel'])
out.append(line)
page_num +=1
if pois_count ==0:
break
return out
def write_in_txt(out):
F = ['店名' , 'ID' , '经营类型', '地区', '地址', '电话']
#删除out列表中的空列表
out_1 = list(filter(None, out))
# 开始写入文本
timemark = str(time.strftime('%Y%m%d%H'))
#写入文本功能开始
output = open('D:/高德地图门店数据_{0}_{1}.txt'.format(timemark,(out_1[0])[3]), 'wb')
title = '\t'.join(F)+'\n'
output.write(title.encode('utf8'))
for l in out_1:
output.write(('\t'.join(l) + '\n').encode('utf8'))
output.close()
#-----------核心功能结束-----------
# print('本次采集已结束!')#此行未显示出来
print('本次共采集:{0}条信息,其中无电话信息已自动删除,具体数据依得到实际数据为准'.format(len(out_1)))#此行未显示出来
#循环翻页执行
while True:
print('欢迎使用通过输入polyline(高德坐标集合)')
polygon = input('输入Q退出\n请输入\n')
out = []
if polygon in ['Q','q']:
print('正在退出...')
time.sleep(5)
break
get_poi(polygon)
if len(out) == 0:
print('本区域无门店可以检索')
continue
else:
write_in_txt(out)
里面还有很多可以优化的,这个是我做测试的版本,功能也算完善了。
需要申请一下自己的key是webAPI一定要注意,调用的是多边形检索 这个接口的数量。控制好调用频率不要超额了。
可以实现的功能:
手动输入两个坐标点,或者坐标合集点,检索区域内的poi数据。要输入的点数量有限不能无限制,测试了很多次也没有发现是多少限制,反正是两个点 四个点 都可以,多点的话最后的那个点要和第一个点相同,不然无法操作。仍需改进的代码块,还需多多升级,不够完善