话不多说,直接开始。

1.页面解析

浏览器搜索 高德地图获取经纬度

python arcpy获取tif经纬度范围 python获取地图上经纬度_经纬度

直接点进去,Chrome浏览的话,直接对着空白右键检查,点到Network

python arcpy获取tif经纬度范围 python获取地图上经纬度_User_02

找到js

这个时候什么也没有,然后在搜索框里进行关键字搜索,比如北京天安门

python arcpy获取tif经纬度范围 python获取地图上经纬度_python_03


搜索过后js下面加载一条text开头的信息,没错点它

python arcpy获取tif经纬度范围 python获取地图上经纬度_json_04


加载过后看到Preview,点它,这里往下面找,可以看到我们需要的经纬度信息就在location这里,name是天安门,没错就是它。

python arcpy获取tif经纬度范围 python获取地图上经纬度_经纬度_05


找到了需要的东西,回到Headers

Request Method: GET这里为GET请求

看到第一个General下面的Request URL这个是我们获取信息的url,复制下来

python arcpy获取tif经纬度范围 python获取地图上经纬度_经纬度_06

我们需要根据地址信息来获取经纬度

这个url比较长,我们可以看看这些参数的“说明”

python arcpy获取tif经纬度范围 python获取地图上经纬度_python_07

这里发现url里面有kewords关键词

https://restapi.amap.com/v3/place/text?s=rsv3&children=&key=6e79f6d236e295632f21b385e363b6e8&offset=1&page=1&extensions=all&city=110000&language=zh_cn&callback=jsonp_383700_&platform=JS&logversion=2.0&appname=https%3A%2F%2Flbs.amap.com%2Ftools%2Fpicker&csid=289E1DA4-BB05-49CE-B620-02C361DC98D5&sdkversion=1.4.15&keywords=%E5%8C%97%E4%BA%AC%E5%A4%A9%E5%AE%89%E9%97%A8

接下来找到 Request Headers,这里我拿的是Cookie,Host,Referer,User-Agent 这四个,至于能不能少拿或者多拿,大家有其他想法可以自己试试

python arcpy获取tif经纬度范围 python获取地图上经纬度_爬虫_08


到此页面解析完毕

2.上代码

import requests
import json
#
address = '北京天安门'
#这里需要把地址换成address,方便后面查询多个地方的经纬度  
url = 'https://restapi.amap.com/v3/place/text?s=rsv3&children=&key=6e79f6d236e295632f21b385e363b6e8&offset=1&page=1&extensions=all&city=110000&language=zh_cn&callback=jsonp_383700_&platform=JS&logversion=2.0&appname=https%3A%2F%2Flbs.amap.com%2Ftools%2Fpicker&csid=289E1DA4-BB05-49CE-B620-02C361DC98D5&sdkversion=1.4.15&keywords={address}'
# 把headers给整好
headers = {
    'Cookie': '你的Cookie'
    ,'Host': 'restapi.amap.com'
    ,'Referer': 'https://lbs.amap.com/'
    ,'User-Agent': '你的User-Agent'
}
# 这里就拿到了信息,打印一下
response = requests.get(url,headers = headers)
print(response.text)

python arcpy获取tif经纬度范围 python获取地图上经纬度_json_09

观察打印结果,发现框里面的有点像json格式,那就切片试试

result = response.text[14:-1]
print(result)

python arcpy获取tif经纬度范围 python获取地图上经纬度_爬虫_10


既然拿到了,就看是不是我们想的那样,直接上json.loads

json_str = json.loads(result)
print(json_str)

python arcpy获取tif经纬度范围 python获取地图上经纬度_json_11


试试没问题

那就直接拿想要的信息

完整代码

import requests
import json

#这里需要把地址换成address,方便后面查询多个地方的经纬度 \是为了换行好看URL太长了
def get_data(address):
    url = f'刚刚复制的url 最后面的keywords={address}'
    # 把headers给整好
    headers = {
        'Cookie': '你的Cookie'
        ,'Host': 'restapi.amap.com'
        ,'Referer': 'https://lbs.amap.com/'
        ,'User-Agent': '你的User-Agent'
    }
    # 这里就拿到了信息,打印一下
    response = requests.get(url,headers = headers)
    result = response.text[14:-1]
    
    json_str = json.loads(result)
    json_str = json.loads(result)
    lo_la = json_str['pois'][0]['location']
    address_name = json_str['pois'][0]['name']
    return f'{address_name}的经纬度是:{lo_la}'

address_list = ['春熙路','天府广场','环球中心']

for address in address_list:
	# 害怕有些地方在不同城市是重名的,所以加个城市在前面稳妥一点
    address = '成都'+address
    data = get_data(address)
    print(data)

python arcpy获取tif经纬度范围 python获取地图上经纬度_经纬度_12