Reputation: 799
I am trying to use some code to download Google street view images.
Code so far is:
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)
I've added this by importing urllib.request
and adding urllib.request.urlretrieve(MyUrl, os.path.join(SaveLoc,fi))
to overcome AttributeError: module ‘urllib’ has no attribute ‘urlretrieve’
.
However am now getting urllib.error.HTTPError: HTTP Error 400: Bad Request
but am not quite sure why or how to fix this.
Upvotes: 0
Views: 973
Reputation: 3856
You're close, but urllib doesn't url-encode parameters by default. You need to call the quoter explicitly. The API is returning 400 because the URL is not correctly formatted when the address has spaces in it. Try adding this:
MyUrl = base + urllib.quote_plus(Add) + key
Note: it looks like you're using Python 2. Python 3 requires:
import urllib.parse
MyUrl = base + urllib.parse.quote_plus(Add) + key
Upvotes: 1