1.pip安装xlrd,xlwt,requests模块.
2.在工程目录处放置地点Excel文件。
python2.7.13代码:
#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 = xlrd.open_workbook('Book2.xlsx')
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, 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结果文件。
PS:py2和Py3有些些不同。
python3.6.1代码
# 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.parse.quote(addr.encode('utf8'))
city = urllib.parse.quote(u'兰州市'.encode('utf8'))
province = urllib.parse.quote(u'甘肃省'.encode('utf8'))
if quote_addr.startswith(city) or quote_addr.startswith(province):
pass
else:
quote_addr = quote_addr
s = urllib.parse.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.text
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, 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()
利用百度地图API,获取经纬度坐标
利用百度地图API,获取经纬度坐标 代码很简单,但在网上没找到现成的获取地图经纬度的页面. 就是想,给当前页面传递一个经纬度,自动定位到此经纬度.然后可以重新选择,选择完返回经纬度. 效果如下: 源代 ...
使用百度地图API自动获取地址和经纬度
先上效果图,这是直接点击获取经纬度和地址的.没有做搜索的功能. 代码: /p>
利用百度地图开源sdk获取地址信息。
注册百度开发者帐号,下载相关sdk 添加权限: 添加百度注册访问应用(AK)码 添加源代码文件到libs文件: 代码如下: package com.lixu.baidu_gps; import com ...
利用百度翻译API,获取翻译结果
利用百度翻译API,获取翻译结果 translate.py #!/usr/bin/python #-*- coding:utf-8 -*- import sys reload(sys) sys.set ...
百度地图API 批量添加 带检索功能的信息窗口
ONST_NAME1"#define CONST_NAME2 "CONST ...
JQuery树形目录制作