最近爬虫了一堆新冠病的数据,想要通过FineBI在地图上展示出来,但是需要自行提供经纬度,网上倒是有部分现成的,比如《Python版中国省市经纬度》,需要可自取。当然还是建议自己爬取,一通百通。
效果如下:
代码功能:
高德上已有相应的开发手册介绍,直接参考其进行相应操作即可。
地理编码/逆地理编码 API:
https://lbs.amap.com/api/webservice/guide/api/georegeo
之前爬的地址数据我放在locs.csv中,字段名称是’省份’和’城市’,文档编码是’gb2312
’
def parse():
datas = []
m = 0
lists=[]
totalListData = pd.read_csv('locs.csv', encoding='gb2312')
totalListDict = totalListData.to_dict('index')
for j in range(0, len(totalListDict)):
datas.append(
#加'|'是为了后面方便批量查询
str(totalListDict[j]['省份']+'省'+str(totalListDict[j]['城市'])+'|'))
#将数组切割为每十个一组
lists = [datas[i:i + 10] for i in range(0, len(datas), 10)]
return lists
将获取到的地址名称通过高德api实现转换;
需设置batch
值为true
,方便批量查询
def transform(cityName):
ak = 'your_key_value' #此处填写自己申请的key值
base = "http://restapi.amap.com/v3/geocode/geo?key=%s&address=%s&batch=true" % (
ak, cityName)
仅仅获取结果还不够,要将我们需要的经纬度信息提取出来。
根据接口文档可知,经纬度信息在geocodes
字段中的location
中;
def transform(k,cityName):
ak = 'yourkey' #被人直接复制粘贴了,搞得我自己没了次数
base = "http://restapi.amap.com/v3/geocode/geo?key=%s&address=%s&batch=true" % (
ak, cityName)
response = requests.get(base)
answer = response. json()
j=0
list=[]
while (j< int(answer['count'])):
# 不过爬取过程中出现有的城市无法确认经纬度的问题,加入判定语句,直接剔除掉
if ((answer['geocodes'] != []) and (answer['geocodes'][j]['district'] != [])):
#省
province=answer['geocodes'][j]['province']
#市
city=answer['geocodes'][j]['city']
location=answer['geocodes'][j]['location']
location=''.join(location).split(',',1)
#经度
jingdu=location[0]
#纬度
weidu=location[1]
#地区
district=answer['geocodes'][j]['district']
df.loc[k] = [province,city,district,jingdu,weidu]
#换行
k=k+1
j=j+1
如下,写入localdetail.csv:
if __name__ == '__main__':
df = pd.DataFrame(columns=['province','city', 'district','jingdu','weidu'])
cityNames = parse()
k=0
for cityName in cityNames:
transform(k,cityName)
k=k+10
df.to_csv('locdetail7.csv', index=False)
《Python 利用高德地图api实现经纬度与地址的批量转换》
《利用高德地图api将实际地址转换为经纬度(高德坐标系)python》
# -*- coding:utf-8 -*-
'''
利用高德地图api实现经纬度与地址的批量转换
'''
import requests
import pandas as pd
import time
import importlib
import sys
import json
importlib.reload(sys)
def parse():
datas = []
m = 0
lists=[]
totalListData = pd.read_csv('Updates_NC.csv', encoding='gb2312')
totalListDict = totalListData.to_dict('index')
for j in range(0, len(totalListDict)):
datas.append(
str(totalListDict[j]['省份']+'省'+str(totalListDict[j]['城市'])+'|'))
lists = [datas[i:i + 10] for i in range(0, len(datas), 10)]
return lists
def transform(k,cityName):
ak = 'your_key_value'
base = "http://restapi.amap.com/v3/geocode/geo?key=%s&address=%s&batch=true" % (
ak, cityName)
response = requests.get(base)
answer = response. json()
j=0
list=[]
while (j< int(answer['count'])):
if ((answer['geocodes'] != []) and (answer['geocodes'][j]['district'] != [])):
province=answer['geocodes'][j]['province']
city=answer['geocodes'][j]['city']
location=answer['geocodes'][j]['location']
location=''.join(location).split(',',1)
jingdu=location[0]
weidu=location[1]
district=answer['geocodes'][j]['district']
df.loc[k] = [province,city,district,jingdu,weidu]
k=k+1
j=j+1
if __name__ == '__main__':
df = pd.DataFrame(columns=['province','city', 'district','jingdu','weidu'])
cityNames = parse()
k=0
for cityName in cityNames:
transform(k,cityName)
k=k+10
df.to_csv('locdetail7.csv', index=False)