#获取json格式的url
http://api.map.baidu.com/geocoder?address=城市&output=json&key=f247cdb592eb43ebac6ccd27f796e2d2
#返回xml格式的url
http://api.map.baidu.com/geocoder?address=城市&output=xml&key=f247cdb592eb43ebac6ccd27f796e2d2
参考资料:
http://www.qingpingshan.com/jb/python/147336.html
http://blog.csdn.net/lanchunhui/article/details/51207696
http://www.cnblogs.com/Lands-ljk/p/5447127.html
# -*- coding: utf-8 -*- import time t1=time.time() from urllib.request import quote from urllib import request, response from xml.dom.minidom import parseString import re from idlelib.iomenu import encoding #中文字符串无法编码成为ascii码 #汉字转码函数 #定义函数 def getlocation(addr): address=quote(addr)#汉字转码 key = 'f247cdb592eb43ebac6ccd27f796e2d2' url='http://api.map.baidu.com/geocoder?address='+address+'&output=xml&key='+key resp=request.urlopen(url) page=resp.read() page=page.decode('utf-8') dom=parseString(page) #提取xml信息 lat=dom.getElementsByTagName('lat')[0].firstChild.data lng = dom.getElementsByTagName('lng')[0].firstChild.data precise= dom.getElementsByTagName('precise')[0].firstChild.data confidence= dom.getElementsByTagName('confidence')[0].firstChild.data level= dom.getElementsByTagName('level')[0].firstChild.data data='%s,%s,%s,%s,%s,%s '%(addr,lat,lng,precise,confidence,level) return data #列表批量提取 file=open('address.txt') f3=open('result.txt','w',encoding='utf-8') lines=file.readlines() print (len(lines)) x=0 for line in lines: str1=line.strip() print (getlocation(str1)) f3.write(getlocation(str1)) if not line: break file.close() t2=time.time() print ('用时:'+str(t2-t1)) #手动输入获取 addr=input('请输入地址:') print ('结果:') print (getlocation(addr)) #写入txt文件 f=open('Info.txt','a') f.write(getlocation(addr))
Bug:
1、输入地址找不到的话程序报错,接下来进行优化
2、设置访问频次,降低出错率
通过经纬度反向获取地理位置
参考资料:
http://blog.csdn.net/xuyw10000/article/details/33342489?utm_source=tuicool&utm_medium=referral
xml:http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=39.983424,116.322987&output=xml&pois=1&ak=TdTGTOUolaMg0jbmfQok4QjZwv5Zxfia
json:http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=39.983424,116.322987&output=json&pois=1&ak=TdTGTOUolaMg0jbmfQok4QjZwv5Zxfia
利用Google Maps Geocoding API
地理编码请求
https://maps.google.com/maps/api/geocode/json?address=河北省秦皇岛市北戴河区&language=zh-CN&sensor=false
1 { 2 "results" : [ 3 { 4 "address_components" : [ 5 { 6 "long_name" : "北戴河区", 7 "short_name" : "北戴河区", 8 "types" : [ "political", "sublocality", "sublocality_level_1" ] 9 }, 10 { 11 "long_name" : "秦皇岛市", 12 "short_name" : "秦皇岛市", 13 "types" : [ "locality", "political" ] 14 }, 15 { 16 "long_name" : "河北省", 17 "short_name" : "河北省", 18 "types" : [ "administrative_area_level_1", "political" ] 19 }, 20 { 21 "long_name" : "中国", 22 "short_name" : "CN", 23 "types" : [ "country", "political" ] 24 } 25 ], 26 "formatted_address" : "中国河北省秦皇岛市北戴河区", 27 "geometry" : { 28 "bounds" : { 29 "northeast" : { 30 "lat" : 39.8894461, 31 "lng" : 119.5413363 32 }, 33 "southwest" : { 34 "lat" : 39.7843424, 35 "lng" : 119.3204744 36 } 37 }, 38 "location" : { 39 "lat" : 39.834596, 40 "lng" : 119.484522 41 }, 42 "location_type" : "APPROXIMATE", 43 "viewport" : { 44 "northeast" : { 45 "lat" : 39.8894461, 46 "lng" : 119.5413363 47 }, 48 "southwest" : { 49 "lat" : 39.7843424, 50 "lng" : 119.3204744 51 } 52 } 53 }, 54 "place_id" : "ChIJXUT4imh_jDURBHiUKgJjOmw", 55 "types" : [ "political", "sublocality", "sublocality_level_1" ] 56 } 57 ], 58 "status" : "OK" 59 }
反向地理编码请求
https://maps.google.com/maps/api/geocode/xml?latlng=39.910093,116.403945&language=zh-CN&sensor=false
https://maps.google.com/maps/api/geocode/json?latlng=39.910093,116.403945&language=zh-CN&sensor=false
1 { 2 "results" : [ 3 { 4 "address_components" : [ 5 { 6 "long_name" : "128", 7 "short_name" : "128", 8 "types" : [ "street_number" ] 9 }, 10 { 11 "long_name" : "南池子大街", 12 "short_name" : "南池子大街", 13 "types" : [ "route" ] 14 }, 15 { 16 "long_name" : "东城区", 17 "short_name" : "东城区", 18 "types" : [ "political", "sublocality", "sublocality_level_1" ] 19 }, 20 { 21 "long_name" : "北京市", 22 "short_name" : "北京市", 23 "types" : [ "locality", "political" ] 24 }, 25 { 26 "long_name" : "北京市", 27 "short_name" : "北京市", 28 "types" : [ "administrative_area_level_1", "political" ] 29 }, 30 { 31 "long_name" : "中国", 32 "short_name" : "CN", 33 "types" : [ "country", "political" ] 34 }, 35 { 36 "long_name" : "100006", 37 "short_name" : "100006", 38 "types" : [ "postal_code" ] 39 } 40 ], 41 "formatted_address" : "中国北京市东城区南池子大街128号 邮政编码: 100006", 42 "geometry" : { 43 "location" : { 44 "lat" : 39.910242, 45 "lng" : 116.403911 46 }, 47 "location_type" : "ROOFTOP", 48 "viewport" : { 49 "northeast" : { 50 "lat" : 39.91159098029149, 51 "lng" : 116.4052599802915 52 }, 53 "southwest" : { 54 "lat" : 39.90889301970849, 55 "lng" : 116.4025620197085 56 } 57 } 58 }, 59 "place_id" : "ChIJCZrNBMdS8DURlC06xmd9ajc", 60 "types" : [ "street_address" ] 61 }, 62 { 63 "address_components" : [ 64 { 65 "long_name" : "东城区", 66 "short_name" : "东城区", 67 "types" : [ "political", "sublocality", "sublocality_level_1" ] 68 }, 69 { 70 "long_name" : "北京市", 71 "short_name" : "北京市", 72 "types" : [ "locality", "political" ] 73 }, 74 { 75 "long_name" : "北京市", 76 "short_name" : "北京市", 77 "types" : [ "administrative_area_level_1", "political" ] 78 }, 79 { 80 "long_name" : "中国", 81 "short_name" : "CN", 82 "types" : [ "country", "political" ] 83 } 84 ], 85 "formatted_address" : "中国北京市东城区", 86 "geometry" : { 87 "bounds" : { 88 "northeast" : { 89 "lat" : 39.9740365, 90 "lng" : 116.4523907 91 }, 92 "southwest" : { 93 "lat" : 39.8586252, 94 "lng" : 116.3771342 95 } 96 }, 97 "location" : { 98 "lat" : 39.92835300000001, 99 "lng" : 116.416357 100 }, 101 "location_type" : "APPROXIMATE", 102 "viewport" : { 103 "northeast" : { 104 "lat" : 39.9740365, 105 "lng" : 116.4523907 106 }, 107 "southwest" : { 108 "lat" : 39.8586252, 109 "lng" : 116.3771342 110 } 111 } 112 }, 113 "place_id" : "ChIJLwKYcTNT8DUR8DZJ7J2CieA", 114 "types" : [ "political", "sublocality", "sublocality_level_1" ] 115 }, 116 { 117 "address_components" : [ 118 { 119 "long_name" : "北京市", 120 "short_name" : "北京市", 121 "types" : [ "locality", "political" ] 122 }, 123 { 124 "long_name" : "北京市", 125 "short_name" : "北京市", 126 "types" : [ "administrative_area_level_1", "political" ] 127 }, 128 { 129 "long_name" : "中国", 130 "short_name" : "CN", 131 "types" : [ "country", "political" ] 132 } 133 ], 134 "formatted_address" : "中国北京市", 135 "geometry" : { 136 "bounds" : { 137 "northeast" : { 138 "lat" : 41.0608157, 139 "lng" : 117.514625 140 }, 141 "southwest" : { 142 "lat" : 39.442758, 143 "lng" : 115.4234112 144 } 145 }, 146 "location" : { 147 "lat" : 39.90419989999999, 148 "lng" : 116.4073963 149 }, 150 "location_type" : "APPROXIMATE", 151 "viewport" : { 152 "northeast" : { 153 "lat" : 40.2164962, 154 "lng" : 116.7829835 155 }, 156 "southwest" : { 157 "lat" : 39.6612714, 158 "lng" : 116.0119343 159 } 160 } 161 }, 162 "place_id" : "ChIJuSwU55ZS8DURiqkPryBWYrk", 163 "types" : [ "locality", "political" ] 164 }, 165 { 166 "address_components" : [ 167 { 168 "long_name" : "100006", 169 "short_name" : "100006", 170 "types" : [ "postal_code" ] 171 }, 172 { 173 "long_name" : "东城区", 174 "short_name" : "东城区", 175 "types" : [ "political", "sublocality", "sublocality_level_1" ] 176 }, 177 { 178 "long_name" : "北京市", 179 "short_name" : "北京市", 180 "types" : [ "locality", "political" ] 181 }, 182 { 183 "long_name" : "北京市", 184 "short_name" : "北京市", 185 "types" : [ "administrative_area_level_1", "political" ] 186 }, 187 { 188 "long_name" : "中国", 189 "short_name" : "CN", 190 "types" : [ "country", "political" ] 191 } 192 ], 193 "formatted_address" : "中国北京市东城区 邮政编码: 100006", 194 "geometry" : { 195 "bounds" : { 196 "northeast" : { 197 "lat" : 39.9294028, 198 "lng" : 116.4193891 199 }, 200 "southwest" : { 201 "lat" : 39.8998095, 202 "lng" : 116.3957813 203 } 204 }, 205 "location" : { 206 "lat" : 39.9117106, 207 "lng" : 116.4097314 208 }, 209 "location_type" : "APPROXIMATE", 210 "viewport" : { 211 "northeast" : { 212 "lat" : 39.9294028, 213 "lng" : 116.4193891 214 }, 215 "southwest" : { 216 "lat" : 39.8998095, 217 "lng" : 116.3957813 218 } 219 } 220 }, 221 "place_id" : "ChIJNZqWMs9S8DUR1mXF08bBnJA", 222 "types" : [ "postal_code" ] 223 }, 224 { 225 "address_components" : [ 226 { 227 "long_name" : "北京市", 228 "short_name" : "北京市", 229 "types" : [ "administrative_area_level_1", "political" ] 230 }, 231 { 232 "long_name" : "中国", 233 "short_name" : "CN", 234 "types" : [ "country", "political" ] 235 } 236 ], 237 "formatted_address" : "中国北京市", 238 "geometry" : { 239 "bounds" : { 240 "northeast" : { 241 "lat" : 41.0608157, 242 "lng" : 117.514625 243 }, 244 "southwest" : { 245 "lat" : 39.442758, 246 "lng" : 115.4234112 247 } 248 }, 249 "location" : { 250 "lat" : 40.2373519, 251 "lng" : 116.2304616 252 }, 253 "location_type" : "APPROXIMATE", 254 "viewport" : { 255 "northeast" : { 256 "lat" : 41.0608157, 257 "lng" : 117.514625 258 }, 259 "southwest" : { 260 "lat" : 39.442758, 261 "lng" : 115.4234112 262 } 263 } 264 }, 265 "place_id" : "ChIJuSwU55ZS8DURp8_dOLuVd8A", 266 "types" : [ "administrative_area_level_1", "political" ] 267 }, 268 { 269 "address_components" : [ 270 { 271 "long_name" : "中国", 272 "short_name" : "CN", 273 "types" : [ "country", "political" ] 274 } 275 ], 276 "formatted_address" : "中国", 277 "geometry" : { 278 "bounds" : { 279 "northeast" : { 280 "lat" : 53.56097399999999, 281 "lng" : 134.7728099 282 }, 283 "southwest" : { 284 "lat" : 17.9996, 285 "lng" : 73.4994136 286 } 287 }, 288 "location" : { 289 "lat" : 35.86166, 290 "lng" : 104.195397 291 }, 292 "location_type" : "APPROXIMATE", 293 "viewport" : { 294 "northeast" : { 295 "lat" : 53.56097399999999, 296 "lng" : 134.7726951 297 }, 298 "southwest" : { 299 "lat" : 18.1618062, 300 "lng" : 73.5034261 301 } 302 } 303 }, 304 "place_id" : "ChIJwULG5WSOUDERbzafNHyqHZU", 305 "types" : [ "country", "political" ] 306 } 307 ], 308 "status" : "OK" 309 }