import requests import urllib import hashlib import pdb def get_lonlat(name): location = name #input("输入所在的位置 ") # "广州市天河区" # 计算校验SN(百度API文档说明需要此步骤) ak = "" # 参照自己的应用 sk = "" # 参照自己的应用 url = "http://api.map.baidu.com" query = "/geocoding/v3/?address={0}&ak={1}&output=json".format(location, ak) data=requests.get(url+query) # pdb.set_trace() data = eval(data.text) # print(data) lat = data["result"]["location"]["lat"] lon = data["result"]["location"]["lng"] print("纬度: ", lat, " 经度: ", lon) return lon,lat if __name__ == '__main__': name='门头沟' get_lonlat(name)