加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

从百度地图API接口批量获取地点的经纬度

发布时间:2020-12-17 17:25:01 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #!/usr/bin/python#coding:utf-8 import xlrdimport xlwtimport requestsimport urllibimport mathimport re pattern_x=re.compile(r'"x":(".+?")')pa

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

#!/usr/bin/python
#coding:utf-8
 
import xlrd
import xlwt
import requests
import urllib
import math
import re
 
pattern_x=re.compile(r'"x":(".+?")')
pattern_y=re.compile(r'"y":(".+?")')
 
def mercator2wgs84(mercator):
    #key1=mercator.keys()[0]
    #key2=mercator.keys()[1]
    point_x=mercator[0]
    point_y=mercator[1]
    x=point_x/20037508.3427892*180
    y=point_y/20037508.3427892*180
    y=180/math.pi*(2*math.atan(math.exp(y*math.pi/180))-math.pi/2)
    return (x,y)
 
def get_mercator(addr):
    quote_addr=urllib.quote(addr.encode('utf8'))
    city=urllib.quote(u'齐齐哈尔市龙'.encode('utf8'))
    province=urllib.quote(u'黑龙江省'.encode('utf8'))
    if quote_addr.startswith(city) or quote_addr.startswith(province):
        pass
    else:
        quote_addr=city+quote_addr
    s=urllib.quote(u'北京市'.encode('utf8'))
    api_addr="http://api.map.baidu.com/?qt=gc&wd=%s&cn=%s&ie=utf-8&oue=1&fromproduct=jsapi&res=api&callback=BMap._rd._cbk62300"%(quote_addr,s)
    req=requests.get(api_addr)
    content=req.content
    x=re.findall(pattern_x,content)
    y=re.findall(pattern_y,content)
    if x:
        x=x[0]
        y=y[0] 
        x=x[1:-1]
        y=y[1:-1]
        x=float(x)
        y=float(y)
        location=(x,y)
    else:
        location=()
    return location
 
def run():
    data=xlrd.open_workbook('Book2.xls')
    rtable=data.sheets()[0]
    nrows=rtable.nrows
    values=rtable.col_values(0)
     
    workbook=xlwt.Workbook()
    wtable=workbook.add_sheet('data',cell_overwrite_ok=True)
    row=0
    for value in values:
        mercator=get_mercator(value)
        if mercator:
            wgs=mercator2wgs84(mercator)
        else:
            wgs=('NotFound','NotFound')
        print "%s,%s,%s"%(value,wgs[0],wgs[1])
        wtable.write(row,value)
        wtable.write(row,1,wgs[0])
        wtable.write(row,2,wgs[1])
        row=row+1
 
    workbook.save('data.xls')
 
if __name__=='__main__':
    run()

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读