测试了百度地图和腾讯地图的api。百度地图没有开放周边地图的web调用接口。所以最后只能用腾讯地图api实现。
参考代码如下
# -*- coding: utf-8 -*-
"""
File Name: gaode_ditu
Description :
Author : meng_zhihao
mail : 312141830@qq.com
date: 2019/12/10
"""
import requests
import json
from urllib.parse import quote, unquote
import csv
import time
api_key = 'xxxxxx' # 高德地图key
def search_place(keyword): # 后续为了更精确建议设置城市
url = 'https://restapi.amap.com/v3/place/text?'
parameters = ''
parameters+='key=%s'%api_key
# parameters+='&types=010000|020000|030000|040000|050000|120000|150000'
parameters+='&keywords=%s'%quote(keyword)
url = url+parameters
rsp = requests.get(url,timeout=20).text
places = json.loads(rsp)
print(places)
firstplace = places['pois']
if firstplace:
firstplace = firstplace[0]
else:
firstplace = {}
return firstplace
def nearby_search(location):
data_set = []
for page in range(1,11,1):
time.sleep(0.2)
url = 'https://restapi.amap.com/v3/place/around?'
parameters = ''
parameters+='key=%s'%api_key
# parameters+='&types=010000|020000|030000|040000|050000|120000|150000'
parameters+='&location=%s'%location
parameters+='&page=%s'%page
parameters+='&offset=%s'%25
url = url+parameters
print(url)
rsp = requests.get(url,timeout=20).text
places = json.loads(rsp)
max_page = int(places['count'])/25+1
data_set.extend(places['pois'])
if page>=max_page:
break
return data_set
if __name__ == '__main__':
while True:
print('输入地名')
keyword = input()
place = search_place(keyword)
print(place)
location = place['location']
pois = nearby_search(location)
with open('pois.csv','a',encoding='utf_8_sig',newline='',) as fout:
writer = csv.writer(fout)
for poi in pois:
data = [poi.get('name'),poi.get('type'),poi.get('address'),poi.get('location')]
writer.writerow(data)
fout.flush()
print('插入完毕,数量 %s'%len(pois))