首页 > 代码库 > Python利用百度地图api批量获取地址经纬度
Python利用百度地图api批量获取地址经纬度
1.pip安装xlrd,xlwt,requests模块.
2.在工程目录处放置地点Excel文件。
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 = 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 = http://www.mamicode.com/xlrd.open_workbook(‘Book2.xlsx‘)"%s,%s,%s" % (value, wgs[0], wgs[1]) wtable.write(row, 0, value) wtable.write(row, 1, wgs[0]) wtable.write(row, 2, wgs[1]) row = row + 1 workbook.save(‘data.xls‘) if __name__ == ‘__main__‘: run()
运行即可得到data.xls结果文件。
Python利用百度地图api批量获取地址经纬度
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。