【问题标题】:Python, Google street view API - HTTP Error 400: Bad RequestPython、Google 街景 API - HTTP 错误 400:错误请求
【发布时间】:2018-03-20 14:24:18
【问题描述】:

我正在尝试使用一些代码来下载 Google 街景图像。

到目前为止的代码是:

import urllib, os
import urllib.request

myloc = r"C:\Users\blah\blah" #replace with your own location
key = "&key=" + "enter your key" #got banned after ~100 requests with no key

def GetStreet(Add,SaveLoc):
  base = "https://maps.googleapis.com/maps/api/streetview?size=400x400&location="
  MyUrl = base + Add + key
  fi = Add + ".jpg"
  urllib.request.urlretrieve(MyUrl, os.path.join(SaveLoc,fi))

Tests = ["457 West Robinwood Street, Detroit, Michigan 48203",
         "1520 West Philadelphia, Detroit, Michigan 48206",
         "2292 Grand, Detroit, Michigan 48238",
         "15414 Wabash Street, Detroit, Michigan 48238",
         "15867 Log Cabin, Detroit, Michigan 48238",
         "3317 Cody Street, Detroit, Michigan 48212",
         "14214 Arlington Street, Detroit, Michigan 48212"]

for i in Tests:
  GetStreet(Add=i,SaveLoc=myloc)

我通过导入urllib.request 并添加urllib.request.urlretrieve(MyUrl, os.path.join(SaveLoc,fi)) 来克服AttributeError: module ‘urllib’ has no attribute ‘urlretrieve’ 来添加它。

但是现在我收到了urllib.error.HTTPError: HTTP Error 400: Bad Request,但我不太清楚为什么或如何解决这个问题。

【问题讨论】:

    标签: python python-3.x google-maps urllib google-street-view


    【解决方案1】:

    你很接近,但 urllib 默认情况下不对参数进行 url 编码。您需要显式调用引用者。 API 返回 400,因为地址中包含空格时 URL 的格式不正确。尝试添加:

    MyUrl = base + urllib.quote_plus(Add) + key
    

    注意:看起来您使用的是 Python 2。Python 3 要求:

    import urllib.parse
    MyUrl = base + urllib.parse.quote_plus(Add) + key
    

    【讨论】:

    • 非常感谢! - 我需要复习一下 urllib!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    相关资源
    最近更新 更多