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': '静安区'}

 

你可能感兴趣的:(爬虫)