python实现百度地图API获取某地址的经纬度

栏目: Python · 发布时间: 8年前

内容简介:python实现百度地图API获取某地址的经纬度

申请百度API

1、打开网页 http://lbsyun.baidu.com/index.php?title=首页

选择功能与服务中的地图,点击左边的获取密匙,然后按照要求申请即可,需要手机和百度账号及邮箱认证。

python实现百度地图API获取某地址的经纬度

python实现百度地图API获取某地址的经纬度

激活后可看到后台页面,现在可以开始创建应用了,这里请求校验方式有两种,一种是白名单IP,另一种是SN校验(旁边有计算的说明),我选择SN校验:

python实现百度地图API获取某地址的经纬度

成功后会获得自己的AK和SK,接下来说明如何计算sn

计算SN(python3.x)

from urllib import parse
import hashlib

def get_urt(addtress):

    # 以get请求为例http://api.map.baidu.com/geocoder/v2/?address=百度大厦&output=json&ak=你的ak
    queryStr = '/geocoder/v2/?address=%s&output=json&ak=你的ak' % addtress

    # 对queryStr进行转码,safe内的保留字符不转换
    encodedStr = parse.quote(queryStr, safe="/:=&?#+!$,;'@()*[]")

    # 在最后直接追加上yoursk
    rawStr = encodedStr + '你的sk'

    #计算sn
    sn = (hashlib.md5(parse.quote_plus(rawStr).encode("utf8")).hexdigest())
    
    #由于URL里面含有中文,所以需要用parse.quote进行处理,然后返回最终可调用的url
    url = parse.quote("http://api.map.baidu.com"+queryStr+"&sn="+sn, safe="/:=&?#+!$,;'@()*[]")   
    
    return url

该url可直接进行进行访问,返回的是json格式,如下

{"status":0,"result":{"location":{"lng":116.50104690641698,"lat":39.79092147361288},"precise":1,"confidence":80,"level":"地产小区"}}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Elements of Statistical Learning

The Elements of Statistical Learning

Trevor Hastie、Robert Tibshirani、Jerome Friedman / Springer / 2009-10-1 / GBP 62.99

During the past decade there has been an explosion in computation and information technology. With it have come vast amounts of data in a variety of fields such as medicine, biology, finance, and mark......一起来看看 《The Elements of Statistical Learning》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具