(编辑:jimmy 日期: 2024/12/27 浏览:2)
本段代码是先将需要转换经纬度的地址爬取在 ‘地址.csv' 文件里,文件截图示例:
代码展示
# coding=utf-8 # SPL # 时间:2020/12/20 21:15 import csv import requests import json import pandas as pd num=0 y=[] with open("地址.csv", 'r') as f: #写入将要转换的地址的文件路径,此处为默认文件路径(要先将文件提前导入)(注意是csv格式文件) r = csv.reader(f, delimiter=',') for row in r: print(row[0]) #记得在key=后面填入申请百度地图开发平台的key url = "http://restapi.amap.com/v3/geocode/geo" + row[0] dat = { 'count': "1", } r = requests.post(url, data=json.dumps(dat)) s = r.json() b = s['geocodes'] for j in range(0, 10000): try: neirong1 = b[j] except: continue try: b = neirong1['location'] except: continue try: lon_lat= b.split(',') lon=float(lon_lat[0]) lat = float(lon_lat[1]) print(lon) print(lat) except: continue num += 1 print("第" + str(num) + "条地址转换成功") y.append([row[0], lon, lat]) result = pd.DataFrame(y) result.columns = ['地址', '经度', '纬度'] result.to_csv('地址转经纬度.csv', encoding='utf-8-sig', index=False)
运行结果
转换成功后生成的表格截图 _