我們都知道,可以使用高德地圖api實(shí)現(xiàn)經(jīng)緯度與地址的轉(zhuǎn)換。那么,當(dāng)我們有很多個(gè)地址與經(jīng)緯度,需要批量轉(zhuǎn)換的時(shí)候,應(yīng)該怎么辦呢?
在這里,選用高德Web服務(wù)的API,其中的地址/逆地址編碼,可以實(shí)現(xiàn)經(jīng)緯度與地址的轉(zhuǎn)換。
高德API地址:
地理/逆地理編碼:http://lbs.amap.com/api/webservice/guide/api/georegeo
坐標(biāo)轉(zhuǎn)換:http://lbs.amap.com/api/webservice/guide/api/convert
1.申請(qǐng)key
2.坐標(biāo)轉(zhuǎn)換
坐標(biāo)轉(zhuǎn)換是一類簡(jiǎn)單的HTTP接口,能夠?qū)⒂脩糨斎氲姆歉叩伦鴺?biāo)(GPS坐標(biāo)、mapbar坐標(biāo)、baidu坐標(biāo))轉(zhuǎn)換成高德坐標(biāo)。
def transform(location): parameters = {'coordsys':'gps','locations': location, 'key': '7ec25a9c6716bb26f0d25e9fdfa012b8'} base = 'http://restapi.amap.com/v3/assistant/coordinate/convert' response = requests.get(base, parameters) answer = response.json() return answer['locations']
2.地理/逆地理編碼
我這里是將經(jīng)緯度轉(zhuǎn)換為地址,所以選用的是逆地理編碼的接口。
def geocode(location): parameters = {'location': location, 'key': '7ec25a9c6716bb26f0d25e9fdfa012b8'} base = 'http://restapi.amap.com/v3/geocode/regeo' response = requests.get(base, parameters) answer = response.json() return answer['regeocode']['addressComponent']['district'].encode('gbk','replace'),answer['regeocode']['formatted_address'].encode('gbk','replace')
3.從文件中讀取
需要批量獲取的話,一般是從文件中讀取數(shù)據(jù),讀取代碼如下:
def parse(): datas = [] totalListData = pd.read_csv('locs.csv') totalListDict = totalListData.to_dict('index') for i in range(0, len(totalListDict)): datas.append(str(totalListDict[i]['centroidx']) + ',' + str(totalListDict[i]['centroidy'])) return datas
4.完整代碼
對(duì)于批量獲取,我一開始也走了很多彎路。一開始選用javascript接口,但是js接口的函數(shù)是異步返回,所以可能第10行的結(jié)果跑到第15行去了,一直沒有很好的解決,后來才選用web接口。最后,將完整代碼貼于此,僅供參考。
#!/usr/bin/env #-*- coding:utf-8 -*- ''' 利用高德地圖api實(shí)現(xiàn)經(jīng)緯度與地址的批量轉(zhuǎn)換 ''' import requests import pandas as pd import time import sys reload(sys) sys.setdefaultencoding("utf-8") def parse(): datas = [] totalListData = pd.read_csv('locs.csv') totalListDict = totalListData.to_dict('index') for i in range(0, len(totalListDict)): datas.append(str(totalListDict[i]['centroidx']) + ',' + str(totalListDict[i]['centroidy'])) return datas def transform(location): parameters = {'coordsys':'gps','locations': location, 'key': '7ec25a9c6716bb26f0d25e9fdfa012b8'} base = 'http://restapi.amap.com/v3/assistant/coordinate/convert' response = requests.get(base, parameters) answer = response.json() return answer['locations'] def geocode(location): parameters = {'location': location, 'key': '7ec25a9c6716bb26f0d25e9fdfa012b8'} base = 'http://restapi.amap.com/v3/geocode/regeo' response = requests.get(base, parameters) answer = response.json() return answer['regeocode']['addressComponent']['district'].encode('gbk','replace'),answer['regeocode']['formatted_address'].encode('gbk','replace') if __name__=='__main__': i = 0 count = 0 df = pd.DataFrame(columns=['location','detail']) #locations = parse(item) locations = parse() for location in locations: dist, detail = geocode(transform(location)) df.loc[i] = [dist, detail] i = i + 1 df.to_csv('locdetail.csv', index =False)
注意事項(xiàng):
在測(cè)試的時(shí)候,一個(gè)key差不多可以下載2000-3000條數(shù)據(jù),一個(gè)賬號(hào)可以申請(qǐng)4個(gè)key。這是我自己的使用情況。所以,測(cè)試的時(shí)候,不用測(cè)試過多,直接開始正式爬數(shù)據(jù)才是正道。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
