Python根據地名獲取經緯度

以下是通過訪問高德地圖接口獲取指定地名的經緯度信息代碼:

import requests
import re
import json

'''獲取地址座標'''
def get_get_location_m(name):
    url="https://restapi.amap.com/v3/place/text?s=rsv3" \
        "&children=&key=8325164e247e15eea68b59e89200988b&page=1" \
        "&offset=10&city=510100&language=zh_cn" \
        "&callback=jsonp_755735_" \
        "&platform=JS&logversion=2.0" \
        "&sdkversion=1.3" \
        "&appname=https%3A%2F%2Flbs.amap.com%2Fconsole%2Fshow%2Fpicker" \
        "&csid=F028E84F-6601-43AE-88A8-13425E3DE7C7" \
        "&keywords={}".format(name)
    res_text=requests.get(url).text
    if re.findall('"info":"OK"',res_text):
        res_data=json.loads(res_text.replace(re.findall("jsonp_\d+_\(",res_text)[0],"")[0:-1])["pois"][0]
        item = {}
        item["name"] = res_data["name"]
        item["type"] = res_data["type"]
        item["location"] = res_data["location"]
        item["pname"] = res_data["pname"]
        item["cityname"] = res_data["cityname"]
        item["adname"] = res_data["adname"]
        return item
    else:
        return None
    
print(get_get_location_m("南京西路  瑞慈悅馨月子會所"))

返回結果如下:

{'name': '瑞慈悅馨月子會所', 'type': '醫療保健服務;醫療保健服務場所;醫療保健服務場所', 'location': '121.449123,31.229171', 'pname': '上海市', 'cityname': '上海市', 'adname': '靜安區'}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章