import json from urllib.request import urlopen from urllib.parse import quote import openpyxl # 定义函数 def Getlngat(address): url = 'https://restapi.amap.com/v3/geocode/geo' output = 'json' # 指定返回数据格式 ak = 'xxxxxxx' add = quote(address) url = url + '?' + 'address=' + add + '&output=' + output + '&key=' +ak # 拼接url # https: // restapi.amap.com / v3 / geocode / geo?address = 北京市朝阳区阜通东大街6号 & output = XML & key = < 用户的key >
# address 是需要获取坐标的结构化地址,output(XML/json)用于指定返回数据的格式,Key是用户请求数据的身份标识
req = urlopen(url) res = req.read().decode() # 将其他编码的字符串解码成unicode temp = json.loads(res) # 对json数据进行分析 return temp # 解析 def Analy(file_path): wb =openpyxl.load_workbook(file_path) ws = wb.active ws.cell(1,2).value='纬度' ws.cell(1,3).value='经度' for i in range(2,ws.max_row+1): try: # 异常捕获发生异常的坐标,不影响后续地点坐标获取 address = ws.cell(i,1).value lng = Getlngat(address)['geocodes'][0]['location'].split(',')[0] # 获取经度 lat = Getlngat(address)['geocodes'][0]['location'].split(',')[1] # 获取纬度 ws.cell(i,2).value = eval(lng) # 写入经度 ws.cell(i,3).value = eval(lat) # 写入纬度 print(address,type(eval(lng)),lat) except: ws.cell(i,4).value = "该地点目前未能获取,请重新尝试" wb.save(file_path) # 主函数 Analy(file_path=r'D:\PyCharm\坐标抓取\地点坐标.xlsx')
详细查看高德开放平台
![](/i/l/?n=23&i=blog/2937168/202302/2937168-20230214094507679-1984910394.png)
标签:经纬度,python,value,cell,json,ws,address,output,高德 From: https://www.cnblogs.com/xujunhui/p/17118608.html