Python调用高德API,实现批量获取地址经纬度,并写入excel文件

调用高德API实现批量输入地址,获取经纬度,并输出到excel表格

# 导入库
import requests,json
import xlsxwriter

# 定义变量
KEY = 'xxx' #此处填入你申请的key
URL_geocode = 'https://restapi.amap.com/v3/geocode/geo?'

# 获取经纬度
def gain_location(address):
    api_url = f'{URL_geocode}city=郑州市&address={address}
    &key={KEY}&output=json&callback=showLocation' #此处城市名可更改
    r = requests.get(api_url)
    r = r.text
    r = r.strip('showLocation(')
    r = r.strip(')')
    jsonData = json.loads(r)['geocodes'][0]['location']
    return jsonData

# 返回信息
def get_place_info(address):
    place_info = []
    for place in address:
            place_point = []
            place_point.append(place)
            location = gain_location(place)
            print(place,location)
            place_point.append(location)
            place_info.append(place_point)
    return place_info

# 自定义地址内容
address = input().split() #此处表示可以输入以空格相隔的地址
#也可以直接写成 address = ['地点1','地点2','地点3'] 的形式
result = get_place_info(address)

# 写入excel
book = xlsxwriter.Workbook('E:/bi/hui/biao.xlsx') #填写创建的excel表的存储地址及表名
sheet = book.add_worksheet()
col = ('place','location')
for i in range(0,2):
    sheet.write(0,i,col[i])
for i in range(0,len(address)):
    for j in range(0,2):
            sheet.write(i+1,j,result[i][j])
book.close()

获取用户key的方式可以在我的上一篇博客中查看~

你可能感兴趣的:(python调用高德API,python,经验分享,交通物流)