获取经纬度
import json from urllib.request import urlopen, quote import pandas as pd import csv xlsx_1 = pd.ExcelFile('bbhu.xlsx') data1 = xlsx_1.parse('Sheet2') url = 'http://api.map.baidu.com/geocoder/v2/' output = 'json' ak = 'raSWR0VKik7******shHzFH' s = data1['小区'] jkdu = [] wwdu = [] # 创建一个worksheet for i in s: try: add = quote(i) uri = url + '?' + 'address=' + add + '&output=' + output + '&ak=' + ak #百度地理编码API req = urlopen(uri) res = req.read().decode() temp = json.loads(res) jkdu.append(temp['result']['location']['lng']) wwdu.append(temp['result']['location']['lat'])#打印出经纬度 except OSError: pass continue result = [] length = len(jkdu) for i in range(length): result.append(str(s[i])+' '+str(jkdu[i])+' '+str(wwdu[i])) with open('xiaoqujkww.csv', 'a', newline='', encoding='utf-8')as f: write = csv.writer(f) for data in result: write.writerow([data]) f.close()
文章来源: https://blog.csdn.net/weixin_43139613/article/details/88871620