为了获取批量地址的经纬度而生的代码~
网上也有一部分在线的网站已经实现了的,但是不是很准。
比如:https://map.yanue.net/
这种呢,部分地区还行,但是有一些是无法识别准确的?
怎么搞呢?
关键字:地址、经纬度、批量?
地址和经纬度能够让你想到什么?
地图?百度地图?高德地图?
批量能让你想到什么?
程序、脚本、代码实现呗?
由于之前小编搞过高德API,那咱去查一下撒~
翻一下接口,就找到了~
接口如下:
https://lbs.amap.com/api/webservice/guide/api/georegeo
用啥子语言搞嘞?
java、python?
搞个脚本就好了呀,所以选择了python
import requests
import pandas as pd
import csv
data = pd.read_csv('c.csv',engine='python')#导入地址的csv文件
data = data['Address']#选择地址列
lis = []
def gaode():
for i in data:
para = {
'key':'507f03ab300b8aa0225a0924e7d12100',
'address':i,
'city':'河南省洛阳市'
}
url = 'https://restapi.amap.com/v3/geocode/geo?'
req = requests.get(url,para)
req = req.json()
if req['infocode']=='10000':
try:
w = req['geocodes'][0]['formatted_address']
z = req['geocodes'][0]['location']
print(w)
print(z)
d = (w, z)
lis.append(d)
except Exception as e:
print (e)
continue
t = ['位置','经纬度']
with open('tset.csv', 'w', newline='')as f:
writer = csv.writer(f)
writer.writerow(t)
writer.writerows(lis)
if __name__ == '__main__':
gaode()
什么bug呢?批量太多了,就不好使了~
因为是是读取文件,全部放到集合里头了,并且一次性写入~
那么怎么解决呢?
批量,批量,批量撒
import requests
import pandas as pd
import csv
data = pd.read_csv('a.csv',engine='python')#导入地址的csv文件
data = data['Address']#选择地址列
lis = []
def gaode(cnt):
flag = 0
# 写表头
writerCsv(0,lis)
print(len(data))
# 遍历数据,调用接口,写数据
for i in data:
para = {
'key':'507f03ab300b8aa0225a0924e7d12100',
'address':i
#'city':'河南省洛阳市'
}
url = 'https://restapi.amap.com/v3/geocode/geo?'
req = requests.get(url,para)
req = req.json()
print(req)
if req['infocode']=='10000':
try:
w = req['geocodes'][0]['formatted_address']
z = req['geocodes'][0]['location']
# print(w)
# print(z)
d = (w, z)
lis.append(d)
flag = flag + 1
except Exception as e:
print (e)
# continue
if flag == cnt:
# print(lis)
writerCsv(1, lis)
lis.clear()
flag = 0
else:
continue
# print(lis)
writerCsv(1,lis)
def writerCsv(flag, list):
if flag == 0 :
t = ['位置','经纬度']
# with open('地址02_经纬度坐标.csv', 'w', newline='')as f:
with open('nanyang_经纬度坐标.csv', 'a+', newline='')as f:
writer = csv.writer(f)
writer.writerow(t)
# writer.writerows(list)
else:
with open('nanyang_经纬度坐标.csv', 'a+', newline='')as f:
writer = csv.writer(f)
writer.writerows(list)
if __name__ == '__main__':
gaode(500)
ok,大功告成撒~
其实代码没有那么难,你要知道自己想要什么,逐渐去完善就好了~
时刻记得自己目标,不要跑偏了就好啦~