地理编码 | 百度地图API SDK (baidu.com)
原始csv
# encoding:utf-8
import requests
import csv
import json
# 接口地址
url = "https://api.map.baidu.com/geocoding/v3"# 此处填写你在控制台-应用管理-创建应用后获取的AK
ak = "XXXXXXX"# 创建CSV文件并写入数据
with open('output22.csv', 'w', encoding='utf-8', newline='') as file:# 创建CSV写入器writer = csv.writer(file)# 打开CSV文件
with open(r"D:\000datasets\链家房价数据\2020去重后社区名称.csv", 'r', encoding='utf-8') as file:# 创建CSV读取器reader = csv.reader(file)# 遍历每一行数据for row in reader:# 打印每一行数据# print(row[0],type(row[0]))params = {"address": row[0],"output": "json","ak": ak,}response = requests.get(url=url, params=params)if response:# print(response.json()['status'],type(response.json()['status']))if response.json()['status']==0:# print("True")print(response.json()) #{'status': 0, 'result': {'location': {'lng': 116.3076223267197, 'lat': 40.05682848596073}, 'precise': 1, 'confidence': 80, 'comprehension': 100, 'level': '门址'}}json_str = json.dumps(response.json())parsed_data = json.loads(json_str)# print(parsed_data, type(parsed_data))# 获取指定字段的内容lng = parsed_data['result']["location"]["lng"]lat = parsed_data['result']["location"]["lat"]# 创建CSV文件并写入数据with open('output22.csv', 'a', encoding='utf-8', newline='') as file:# 创建CSV写入器writer = csv.writer(file)writer.writerow([row[0],lng,lat,parsed_data['result']['precise'],parsed_data['result']['confidence'],parsed_data['result']['comprehension'],parsed_data['result']['level']])
结果:
字段名称:
社区名称、经度、纬度、看下表吧。
地理编码 | 百度地图API SDK (baidu.com)
或者参考这篇博文:
http://t.csdn.cn/AghZk