Python调用百度地图API实现批量经纬度转换为实际省市地点(api调用,json解析,excel读取与写入)

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组数据为例)

初始的经纬度信息:

批量解析经纬度之后的位置信息:

原文地址:https://www.cnblogs.com/shixinzei/p/11029279.html