如何使用 Google API 从具有坐标的数据集中获取地址?
How to get addresses from dataset having coordinates using Google 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 Traceback (most recent call
last) in ()
8 for i in range(len(coordinates)):
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 page = urllib.request.urlopen(qpage)
11 data = page.read().decode('utf-8').replace('(','[').replace(')',']')
12 data=data[34:]
c:\users\anish\appdata\local\programs\python\python36\lib\urllib\request.py
in urlopen(url, data, timeout, cafile, capath, cadefault, context)
221 else:
222 opener = _opener
--> 223 return opener.open(url, data, timeout)
224
225 def install_opener(opener):
c:\users\anish\appdata\local\programs\python\python36\lib\urllib\request.py
in open(self, fullurl, data, timeout)
530 for processor in self.process_response.get(protocol, []):
531 meth = getattr(processor, meth_name)
--> 532 response = meth(req, response)
533
534 return response
c:\users\anish\appdata\local\programs\python\python36\lib\urllib\request.py
in http_response(self, request, response)
640 if not (200 <= code < 300):
641 response = self.parent.error(
--> 642 'http', request, response, code, msg, hdrs)
643
644 return response
c:\users\anish\appdata\local\programs\python\python36\lib\urllib\request.py
in error(self, proto, *args)
568 if http_err:
569 args = (dict, 'default', 'http_error_default') + orig_args
--> 570 return self._call_chain(*args)
571
572 # XXX probably also want an abstract factory that knows when it makes
c:\users\anish\appdata\local\programs\python\python36\lib\urllib\request.py
in _call_chain(self, chain, kind, meth_name, *args)
502 for handler in handlers:
503 func = getattr(handler, meth_name)
--> 504 result = func(*args)
505 if result is not None:
506 return result
c:\users\anish\appdata\local\programs\python\python36\lib\urllib\request.py
in http_error_default(self, req, fp, code, msg, hdrs)
648 class HTTPDefaultErrorHandler(BaseHandler):
649 def http_error_default(self, req, fp, code, msg, hdrs):
--> 650 raise HTTPError(req.full_url, code, msg, hdrs, fp)
651
652 class HTTPRedirectHandler(BaseHandler):
HTTPError: HTTP Error 403: Forbidden
如有任何帮助,我们将不胜感激。
你用的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 地图 JavaScript API 的地理编码服务的内部调用。你不应该使用内部 URLs,使用官方网络服务调用。
查看 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]))
希望对您有所帮助!
数据集有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 Traceback (most recent call last) in () 8 for i in range(len(coordinates)): 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 page = urllib.request.urlopen(qpage) 11 data = page.read().decode('utf-8').replace('(','[').replace(')',']') 12 data=data[34:]
c:\users\anish\appdata\local\programs\python\python36\lib\urllib\request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context) 221 else: 222 opener = _opener --> 223 return opener.open(url, data, timeout) 224 225 def install_opener(opener):
c:\users\anish\appdata\local\programs\python\python36\lib\urllib\request.py in open(self, fullurl, data, timeout) 530 for processor in self.process_response.get(protocol, []): 531 meth = getattr(processor, meth_name) --> 532 response = meth(req, response) 533 534 return response
c:\users\anish\appdata\local\programs\python\python36\lib\urllib\request.py in http_response(self, request, response) 640 if not (200 <= code < 300): 641 response = self.parent.error( --> 642 'http', request, response, code, msg, hdrs) 643 644 return response
c:\users\anish\appdata\local\programs\python\python36\lib\urllib\request.py in error(self, proto, *args) 568 if http_err: 569 args = (dict, 'default', 'http_error_default') + orig_args --> 570 return self._call_chain(*args) 571 572 # XXX probably also want an abstract factory that knows when it makes
c:\users\anish\appdata\local\programs\python\python36\lib\urllib\request.py in _call_chain(self, chain, kind, meth_name, *args) 502 for handler in handlers: 503 func = getattr(handler, meth_name) --> 504 result = func(*args) 505 if result is not None: 506 return result
c:\users\anish\appdata\local\programs\python\python36\lib\urllib\request.py in http_error_default(self, req, fp, code, msg, hdrs) 648 class HTTPDefaultErrorHandler(BaseHandler): 649 def http_error_default(self, req, fp, code, msg, hdrs): --> 650 raise HTTPError(req.full_url, code, msg, hdrs, fp) 651 652 class HTTPRedirectHandler(BaseHandler):
HTTPError: HTTP Error 403: Forbidden
如有任何帮助,我们将不胜感激。
你用的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 地图 JavaScript API 的地理编码服务的内部调用。你不应该使用内部 URLs,使用官方网络服务调用。
查看 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]))
希望对您有所帮助!