python地理编码将地址信息转化为经纬度坐标。
import requests import json import time # 高德地址编码web-API def get_urls(add): url = "https://restapi.amap.com/v3/geocode/geo?address=" + str(add) url = url + "&output=json&key=4d9a765939a2b765**" r = requests.get(url) res = json.loads(r.text) # 数据格式化; content = str(add) + "," + str(res['geocodes'][0]['location']) + "\n" # 写入文件 with open(r'data.csv', 'a') as d: d.write(content) return "" # 读取地址信息; with open(r'add.txt', 'r', encoding='utf-8') as f: # 读取地址信息列表 address = f.read().split() # 地址转换 i = 0 for item in address: # 调用API封装函数; get_urls(item) print("第{}个地址已转换".format(i + 1)) if i % 99 == 0 and i > 0: print("第", i + 1, "个数据,执行延迟策略.") time.sleep(1) i += 1
lockdatav Done!