如何使用Google API从具有坐标的数据集中获取地址?

时间:2018-10-06 04:56:38

标签: python python-3.x google-maps web-scraping google-geocoding-api

数据集具有9975个经度和纬度。我想提取地址。我已经编写了以下代码:

import numpy as np
from bs4 import BeautifulSoup
import urllib.request
import json

coordinates=coordinates.as_matrix()
address=[]
for i in range(len(coordinates)):
    qpage = 'https://maps.googleapis.com/maps/api/js/GeocodeService.Search?5m2&1d'+str(coordinates[i][0])+'&2d'+str(coordinates[i][1])+'&7sUS&9sen&callback=_xdc_._jhwtgt&key=MY_API_KEY&token=53066'
    page = urllib.request.urlopen(qpage)
    data = page.read().decode('utf-8').replace('(','[').replace(')',']')
    data=data[34:]
    js = json.loads(data)
    address.append(js[0]['results'][1]['formatted_address'])

我得到的错误:

  

HTTPError跟踪(最近的调用)   最后)在()         i在范围内(len(坐标))为8:         9 qpage ='https://maps.googleapis.com/maps/api/js/GeocodeService.Search?5m2&1d'+ str(coordinates [i] [0])+'&2d'+ str(coordinates [i] [1])+'&7sUS&9sen&callback = xdc ._ jhwtgt&key = MY_API_KEY&token = 53066'   ---> 10页= urllib.request.urlopen(qpage)        11个数据= page.read()。decode('utf-8')。replace('(','[')。replace(')',']')        12个数据=数据[34:]

     

c:\ users \ anish \ appdata \ local \ programs \ python \ python36 \ lib \ urllib \ request.py   在urlopen中(URL,数据,超时,cafile,capath,cadefault,上下文)       221其他:       222开瓶器= _opener   -> 223返回opener.open(URL,数据,超时)       224       225 def install_opener(开启):

     

c:\ users \ anish \ appdata \ local \ programs \ python \ python36 \ lib \ urllib \ request.py   在打开状态(自我,完整网址,数据,超时)       self.process_response.get(protocol,[])中的处理器为530:       第531章真相(二更)   -> 532响应= meth(req,响应)       533       534返回响应

     

c:\ users \ anish \ appdata \ local \ programs \ python \ python36 \ lib \ urllib \ request.py   在http_response中(自己,请求,响应)       如果不是,则为640(200 <=代码<300):       第641章;   -> 642'http',请求,响应,代码,msg,hdrs)       643       644返回响应

     

c:\ users \ anish \ appdata \ local \ programs \ python \ python36 \ lib \ urllib \ request.py   错误(自我,原型,* args)       第568章(五更)       569 args =(dict,'default','http_error_default')+ orig_args   -> 570返回self._call_chain(* args)       571       572#XXX可能还想要一个抽象工厂,该工厂知道何时创建

     

c:\ users \ anish \ appdata \ local \ programs \ python \ python36 \ lib \ urllib \ request.py   在_call_chain(self,chain,kind,meth_name,* args)中       处理程序中的处理程序502:       503 func = getattr(handler,meth_name)   -> 504结果= func(* args)       505,如果结果不是None:       506返回结果

     

c:\ users \ anish \ appdata \ local \ programs \ python \ python36 \ lib \ urllib \ request.py   在http_error_default(self,req,fp,code,msg,hdrs)中       648类HTTPDefaultErrorHandler(BaseHandler):       649 def http_error_default(self,req,fp,code,msg,hdrs):   -> 650引发HTTPError(req.full_url,code,msg,hdrs,fp)       651       652类HTTPRedirectHandler(BaseHandler):

     

HTTPError:HTTP错误403:禁止

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

您使用的URL

'https://maps.googleapis.com/maps/api/js/GeocodeService.Search?5m2&1d'+str(coordinates[i][0])+'&2d'+str(coordinates[i][1])+'&7sUS&9sen&callback=_xdc_._jhwtgt&key=YOUR_API_KEY&token=53066'

这是Google Maps JavaScript API的内部地理编码服务调用。您不应使用内部URL,而应使用正式的Web服务调用。

请查看Geocoding API文档,并将该URL替换为已记录的反向地理编码URL:

'https://maps.googleapis.com/maps/api/geocode/json?latlng='+str(coordinates[i][0])+'%2C'+str(coordinates[i][1])+'&key=YOUR_API_KEY

我认为您收到403错误,因为请求中的令牌已过期。该令牌是由Maps JavaScript API生成的,因此您应该使用网络服务调用才能解决该问题。

请注意,Web服务每秒只能查询50个查询。

此外,我建议您看看Python Client for Google Maps Services。使用此库,您可以轻松地对坐标进行地理编码

import googlemaps

coordinates=coordinates.as_matrix()
gmaps = googlemaps.Client(key='YOUR_API_KEY')

for i in range(len(coordinates)):
    reverse_geocode_result = gmaps.reverse_geocode((coordinates[i][0], coordinates[i][1]))

我希望这会有所帮助!