qicaoyu
 1 # _*_ coding:utf-8 _*_
 2 import requests
 3 import json
 4 import codecs
 5 from openpyxl import Workbook
 6 wb = Workbook()
 7 sheet = wb.active
 8 sheet.title = "qiang"
 9 def get_location(address, i):
10     print(i)
11     url = "http://restapi.amap.com/v3/geocode/geo"
12     data = {
13         \'key\': \'36e40d76f8a91bc9de0b56eeca4b650b\', #在高德地图开发者平台申请的key,需要替换为自己的key
14         \'address\': address
15     }
16     r = requests.post(url, data=data).json()
17     sheet["A{0}".format(i)].value = address.strip(\'\n\')
18     print(r)
19     if r[\'status\'] == \'1\':
20         if len(r[\'geocodes\']) > 0:
21             GPS = r[\'geocodes\'][0][\'location\']
22             sheet["B{0}".format(i)].value = \'[\' + GPS +\']\'
23         else:
24             sheet["B{0}".format(i)].value = \'[]\'   
25     else:
26        sheet["B{0}".format(i)].value = \'未找到\' 
27 #将地址信息替换为自己的文件,一行代表一个地址,根据需要也可以自定义分隔符
28 f = codecs.open(r"地址信息.txt", "r", "utf-8")
29 i = 0
30 while True:
31     line = f.readline()
32     i = i + 1
33     if not line:
34         f.close()
35         wb.save(r"保存文件.xlsx")
36         break
37     get_location(line, i)

 

分类:

技术点:

相关文章: