I written a code for downloading and saving images from a site .It worked nicely,but for some urls it is being showing an error.I have paste code below
import urllib2
import webbrowser
imageurl='http://www.example.com/'+image[s]
opener1 = urllib2.build_opener()
page1=opener1.open(imageurl)
my_picture=page1.read()
image1=image[s].replace("/","")
fout = open('images/tony/'+image1, "wb")
fout.write(my_picture)
fout.close()
Actually i get many values of image[s] and is working almost condition.But when the value of image[s]=images/PG013001 GROUP 2.jpg, the compiler gives an error
File "leather.py", line 37, in get_leather
page1=opener1.open(imageurl)
File "D:\Program Files\Python\lib\urllib2.py", line 395, in open
response = meth(req, response)
File "D:\Program Files\Python\lib\urllib2.py", line 508, in http_response
'http', request, response, code, msg, hdrs)
File "D:\Program Files\Python\lib\urllib2.py", line 433, in error
return self._call_chain(*args)
File "D:\Program Files\Python\lib\urllib2.py", line 367, in _call_chain
result = func(*args)
File "D:\Program Files\Python\lib\urllib2.py", line 516, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: Not Found
I thought the corresponding imageurl ie ‘http://www.example.com/images/PG013001 GROUP 2.jpg’ doesn,t exist but when it is checked it exist.Please suggest a fix
regards
Urls can’t include spaces directly; it’s simply not allowed. What you want to do is to quote, or encode the spaces in the filename, so that the url becomes legal. Here’s wikipedia on the matter.
So, you want to quote the urls that you pass to urllib2. In your code, you could do that by changing the one line to look like this:
That oughta do it.