先申请高德API,Web服务的Key,然后根据http://lbs.amap.com/api/webservice/guide/api/georegeo信息,
填写parameters,在利用requests模块,获取url,读取json文件,在利用正则表达式分割,获取lat/lng
import requests
import re
def getcode(site):
parameters = { 'address' : site, 'key': 'bdxxxxxxxxxxx'}
base = 'http://restapi.amap.com/v3/geocode/geo'
response = requests.get(base, parameters)
info_site = response.json()
return info_site
#print(info_site)
#if __name__ == '__main__':
# address = '广州天河区'
# getcode(address)
def get_site(info):
lat = info['geocodes'][0]['location'].split(',')[0]
lng = info['geocodes'][0]['location'].split(',')[1]
return lat, lng
address = getcode('广州天河区')
#print(address)
get_site(address)
输出
('113.361200', '23.124680')