1.獲取秘鑰
調用百度地圖API實現得申請百度賬號或者登陸百度賬號,然后申請自己的ak秘鑰。鏈接如下:http://lbsyun.baidu.com/apiconsole/key?application=key
2.調用API將經緯度信息解析成json信息
3.可以自行通過json格式選擇自己想要的數據,比如國家、省份、市區等。
4.批量轉換信息
先要批量讀取經緯度信息,我是將經緯度信息存在excel表中,然后再通過調用openpyxl中的函數讀取經緯度信息。
讀取完經緯度信息之后,再for循環遍歷每個經緯度,然后再通過調用xlwt中的寫入excel的方法存放具體的位置信息。
5.注意事項
調用百度地圖API獲取經緯度信息是有每天的請求額度限制。
最開始我不想自己編程搞的,但是借用他站的批量解析經緯度的速度實在太慢,於是我打算自己調用api寫程序批量導入寫出信息。
我是打算一次性跑兩千多的數據,但是跑了幾次,最多的時候跑到一千多組,就給我說額度不夠了。。。很無奈。。。但是這次還是挺有收獲的。。。
1 #encoding=utf8 2 import json 3 import urllib.request 4 import openpyxl 5 import xlwt 6 #基於百度地圖API下的經緯度信息來解析地理位置信息 7 def getlocation(lat,lng): 8 url = 'http://api.map.baidu.com/geocoder/v2/?location=' + lat + ',' + lng + '&output=json&pois=1&ak=自己的ak秘鑰' 9 req = urllib.request.urlopen(url) # json格式的返回數據 10 res = req.read().decode("utf-8") # 將其他編碼的字符串解碼成unicode 11 return json.loads(res) 12 13 def jsonFormat(lat,lng): 14 str = getlocation(lat,lng) 15 dictjson={}#聲明一個字典 16 #get()獲取json里面的數據 17 jsonResult = str.get('result') 18 address = jsonResult.get('addressComponent') 19 #國家 20 country = address.get('country') 21 #國家編號(0:中國) 22 country_code = address.get('country_code') 23 #省 24 province = address.get('province') 25 #城市 26 city = address.get('city') 27 #城市等級 28 city_level = address.get('city_level') 29 #縣級 30 district = address.get('district') 31 #把獲取到的值,添加到字典里(添加) 32 # dictjson['country']=country 33 # dictjson['country_code'] = country_code 34 dictjson['province'] = province 35 dictjson['city'] = city 36 # dictjson['city_level'] = city_level 37 # dictjson['district']=district 38 return dictjson 39 40 def read_xls(): 41 wb = openpyxl.load_workbook('TestLocation.xlsx') 42 sheets = wb.sheetnames 43 # print(sheets, type(sheets)) 44 # print(wb.sheetnames) 45 sheet1 = wb.get_sheet_by_name('Sheet1') 46 # for sheet in wb: 47 # print(sheet.title) 48 ws = wb.active # 當前活躍的表單 49 col_range = ws['B:C'] 50 row_range = ws[2:2034] 51 # for col in col_range: # 打印BC兩列單元格中的值內容 52 # for cell in col: 53 # print(cell.value) 54 # for row in row_range: # 打印 2-2034行中所有單元格中的值 55 # for cell in row: 56 # print(cell.value) 57 k = 1 58 # 創建Workbook 59 book = xlwt.Workbook(encoding='utf-8') # 創建Workbook,相當於創建Excel= 60 sheet2 = book.add_sheet(u'Sheet1', cell_overwrite_ok=True) 61 for i,j in ws.iter_rows(min_row=2, max_row=2034, min_col=2,max_col=3): # 打印1-2行,1-2列中的內容 62 # for cell in row: 63 # print(cell.value) 64 # print(cell.value) 65 print(i.value,j.value) # 經緯度 66 print(jsonFormat(str(j.value),str(i.value))) # 通過經緯度調用函數獲取位置 67 # print(jsonFormat(str(j.value),str(i.value))['province'],jsonFormat(str(j.value),str(i.value))['city']) 68 # 將位置寫入excel表中 69 sheet2.write(k,3,k) 70 sheet2.write(k,0,jsonFormat(str(j.value),str(i.value))['province']) 71 sheet2.write(k,1,jsonFormat(str(j.value),str(i.value))['city']) 72 book.save('Test_Write.xls') 73 k = k + 1 74 print(k) 75 book.save('Test_Write.xls') 76 77 if __name__ == '__main__': 78 # print(getlocation(lat, lng)) 79 # print("------------------") 80 # print(jsonFormat(lat,lng)) 81 read_xls()
測試數據和運行結果如下:(以20組數據為例)
初始的經緯度信息:
批量解析經緯度之后的位置信息: