【发布时间】:2019-03-11 13:32:57
【问题描述】:
数据集有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(最近调用 最后)在() 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 页 = 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 开瓶器 = _开瓶器 --> 223 返回 opener.open(url, data, timeout) 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, response) 533 534返回响应
c:\users\anish\appdata\local\programs\python\python36\lib\urllib\request.py 在 http_response(self, request, response) 如果不是,则为 640(200 642 'http', 请求, 响应, 代码, msg, hdrs) 643 644返回响应
c:\users\anish\appdata\local\programs\python\python36\lib\urllib\request.py 错误(自我,原型,*args) 568 如果 http_err: 第569章 --> 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章 --> 504 结果 = func(*args) 如果结果不是无,则为 505: 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章 --> 650 引发 HTTPError(req.full_url, code, msg, hdrs, fp) 651 652类HTTPRedirectHandler(BaseHandler):
HTTPError:HTTP 错误 403:禁止
任何帮助将不胜感激。
【问题讨论】:
标签: python python-3.x google-maps web-scraping google-geocoding-api