运用python获取当前位置的经纬度

分享 python代码块  收藏
1 / 889

import requests

AK = "apikey" # 把复制的 AK 直接粘贴过来就可以了

def change(name):
url = 'http://api.map.baidu.com/geocoding/v3/?address=%s&output=json&ak=%s' % (name, AK)
res = requests.get(url)
if res.status_code == 200:
val = res.json()
if val["status"] == 0:
retval = {'地址': name, '经度': val['result']['location']['lng'], '纬度': val['result']['location']['lat'],
'地区标签': val['result']['level'], '是否精确查找': val['result']['precise']}
else:
retval = None
return retval
else:
print('无法获取 %s 经纬度' % name)

if name == 'main':
print(change('徐家汇'))