本段代码是先将需要转换经纬度的地址爬取在 ‘地址.csv' 文件里,文件截图示例:
代码展示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# 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?key=**********&address=" + 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)
|
运行结果
转换成功后生成的表格截图 _
到此这篇关于python调用高德api实现批量地址转经纬度并写入表格的功能的文章就介绍到这了,更多相关python高德api实现批量地址转经纬度内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/gjgfjgy/article/details/112443653