python调用百度接口解析/逆解析地址
一、创建应用:
首先进入百度地图开发平台官网:http://lbsyun.baidu.com/index.php?title=首页
注册账号以及创建应用,这里不做详细说明。
二、js中简单调用百度接口:
官方参考文档:http://lbsyun.baidu.com/index.php?title=jspopular/guide/helloworld
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Hello, World</title> <style type="text/css"> html{height:100%} body{height:100%;margin:0px;padding:0px} #container{height:100%} </style> <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"> //v2.0版本的引用方式:src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥" </script> </head> <body> <div id="container"></div> <script type="text/javascript"> var map = new BMap.Map("container"); // 创建地图实例 var point = new BMap.Point(116.404, 39.915); // 创建点坐标 map.centerAndZoom(point, 15); // 初始化地图,设置中心点坐标和地图级别 </script> </body> </html>
三、实际应用:
通过从数据库中获取单位的所在地区的原始数据,进行简单的整理之后加入URL地址中
URL进行如下所示的拼接,可以进行地址解析,获取地区的百度经纬度坐标
url = 'http://api.map.baidu.com/geocoding/v3/?address={Address}&output=json&ak={Ak}'.format(Address=地区,Ak=你的ak)
URL进行如下所示的拼接,可以进行逆地址解析,获取百度经纬度坐标的结构化地址
url = 'http://api.map.baidu.com/reverse_geocoding/v3/?ak='+你的ak+'&output=json&coordtype=wgs84ll&location='+str(纬度)+','+str(经度)
其中转换类Geocoder:用于坐标与地址间的相互转换
下面是项目代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import requests import json import pymysql ak='你的ak' def mysql_(): conn = pymysql.connect(host='127.0.0.1', user='数据库账号', passwd='数据库密码', db='数据库', port=3306, charset='utf8', cursorclass=pymysql.cursors.DictCursor) cur = conn.cursor() return conn,cur def selectdiyu(): conn, cur = mysql_() sql = "select id,complete from chengguo" cur.execute(sql) diyus=cur.fetchall() return diyus def updatediyu(diyu, adcode, id): data = [] data.append((diyu, adcode, id)) conn, cur = mysql_() sql = "update chengguo set diyu = %s,adcode = %s where id = %s" cur.executemany(sql,data) conn.commit() def updatecode(adcode, id): data = [] data.append((adcode, id)) conn, cur = mysql_() sql = "update chengguo set adcode = %s where id = %s" cur.executemany(sql,data) conn.commit() def getPosition(url): res = requests.get(url) json_data = json.loads(res.text) if json_data['status'] == 0: lat = json_data['result']['location']['lat'] # 纬度 lng = json_data['result']['location']['lng'] # 经度 else: print("Error output!") return json_data['status'] return lat, lng def getlocation(lat, lng, id): url = 'http://api.map.baidu.com/reverse_geocoding/v3/?ak='+ak+'&output=json&coordtype=wgs84ll&location='+str(lat)+','+str(lng) result = requests.get(url) url_text = result.content.decode() text = json.loads(url_text) address = text.get('result').get('addressComponent') province = address.get('province') city = address.get('city') district = address.get('district') adcode = address.get('adcode') if province == city: diyu = province + district else: diyu = province + city + district try: # updatediyu(diyu, adcode, id) updatecode(adcode, id) print('更新成功',id,diyu,adcode) except: print('更新失败') if __name__ == "__main__": diyus = selectdiyu() for diyu in diyus: add = diyu['complete'].replace('等','').split(' ')[0] add = add.split('、')[0].partition('项目负责人')[0] add_url = 'http://api.map.baidu.com/geocoding/v3/?address={Address}&output=json&ak={Ak}'.format(Address=add,Ak=ak) try: lat, lng = getPosition(add_url) getlocation(lat, lng, diyu['id']) except: add_url = 'http://api.map.baidu.com/geocoding/v3/?address={Address}&output=json&ak={Ak}'.format(Address='天津市'+add,Ak=ak) lat, lng = getPosition(add_url) getlocation(lat, lng, diyu['id'])