首页 > 编程语言 >python之调用高德、百度api解析经纬度地址

python之调用高德、百度api解析经纬度地址

时间:2022-12-22 10:12:04浏览次数:48  
标签:coordStrings return 经纬度 python resp api dict 高德

调用高德

# 高德地图根据经纬度反查地址,每天只能调用5000次
def gaode_excute_single_query(coordStrings ,currentkey='你自己的api-key'):
    # 1-将coordList中的经纬度坐标连接成高德地图API能够识别的样子
    #    coordString+= f"{long},{lat}|"
    # 2-地理编码查询需要的Url
    output = 'json'    # 查询返回的形式
    batch = 'true'     # 是否支持多个查询
    base = 'https://restapi.amap.com/v3/geocode/regeo?'    # 逆地理编码查询Url的头
    currentUrl = base + "output=" + output + "&batch=" + batch + "&location=" + coordStrings+ "&key=" + currentkey
    # 3-提交请求
    response = requests.get(currentUrl)    # 提交请求
    answer = response.json()   # 接收返回
    # 4-解析Json的内容
    resultList = []    # 用来存放逆地理编码结果的空序列
    if answer['status'] == '1' and answer['info'] == 'OK':
        # 4.1-请求和返回都成功,则进行解析
        tmpList = answer['regeocodes']    # 获取所有结果坐标点
        for i in range(0,len(tmpList)):
            try:
                # 解析','分隔的经纬度
                addressString = tmpList[i]['formatted_address']
                # 放入结果序列
                resp_dict=dict()
                resp_dict[coordStrings]=addressString
                resultList.append(resp_dict)
            except:
                # 如果发生错误则存入None
                traceback.print_exc()
                resultList.append(None)
        return resultList
    elif answer['info'] == 'DAILY_QUERY_OVER_LIMIT':
        # 4.2-当前账号的余额用完了,返回-1
        return -1
    else:
        # 4.3-如果发生其他错误则返回-2
        return -2

调用百度

# 百度地图根据经纬度反查地址,每天只能调用5000次
def baidu_excute_single_query(coordStrings,ak='你自己的api-key'):
    # coordStrings:格式 纬度,经度 f'{lat},{long}'
    url=f'https://api.map.baidu.com/reverse_geocoding/v3/?ak={ak}&output=json&coordtype=wgs84ll&location={coordStrings}&'
    resp=requests.get(url=url,params=None).json()
    # print(resp)
    if resp.get('result') :
        res_dict=dict()
        res_dict[coordStrings]=resp['result']['formatted_address']
        return res_dict
    return f"{coordStrings},解析失败"

标签:coordStrings,return,经纬度,python,resp,api,dict,高德
From: https://www.cnblogs.com/qtclm/p/16997747.html

相关文章

  • python实现计算精度、召回率和F1值
    python实现计算精度、召回率和F1值  摘要:在深度学习的分类任务中,对模型的评估或测试时需要计算其在验证集或测试集上的预测精度(prediction/accuracy)、召回率(recall)和F1值......
  • 关于python网络爬虫——摘取新闻标题及链接
    Python是最近流行的编程语言,最近学习了python编程,并在网络爬虫方面进行了研究,下面给出简单实例来实现网络信息的获取步骤一:要有python开发环境,可下载python3.5版本,或anacond......
  • Python之异常
    一、了解异常当检测到⼀个错误时,解释器就⽆法继续执⾏了,反⽽出现了⼀些错误的提示,这就是所谓的"异常"。例如:以r⽅式打开⼀个不存在的⽂件。open('test.txt','r')二、异常......
  • Python之模块和包
    一、模块Python模块(Module),是⼀个Python⽂件,以.py结尾,包含了Python对象定义和Python语句。模块能定义函数,类和变量,模块⾥也能包含可执⾏的代码。1、导入模块1、导入......
  • MyBatis的相应API与传统和代理开发的Dao层实现
    MyBatis的相应API1、SqlSession工厂构建器SqlSessionFactoryBuilder常用API:SqlSessionFactorybuild(InputStreaminputStream)通过加载mybatis的核心文件的输入流的形式构......
  • python爬虫如何模拟正常用户的访问行为?
    在Python爬虫抓取数据时,我们很容易被目标网站限制访问,这是目标网站阻止别人批量获取自己网站信息的一种方式,通常会采用封IP作为终极手段,效果非常好。针对此类情况,作为Py......
  • .net core web api 路由约束
    路由约束在传入URL发生匹配时执行,URL路径标记为路由值。路径约束通常检查通过路径模板关联的路径值,并对该值是否为可接受做出对/错决定。某些路由约束使用路由值以外......
  • 腾讯大佬告诉你,写Python到底用什么IDE合适
    OverridetheentrypointofanimageIntroducedinGitLabandGitLabRunner9.4.Readmoreaboutthe extendedconfigurationoptions.Beforeexplainingtheav......
  • Ceiling Division in Python
    ToperformceilingdivisioninPython,youcandefineyourownfunctionandutilizethefloordivisionoperator //.>>>defceiling_division(x,y):...ret......
  • ptrade 量化交易接口 API接口文档
    http://ptradeapi.com  更新了多个可转债数据接口比如:ptradeapi #可转债溢价率规模数据 ......