0

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

2 Answers 2

1

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:

page1=opener1.open(urllib2.quote(imageurl))

That oughta do it.

Sign up to request clarification or add additional context in comments.

Comments

1

You should fix the link. Try this:

>>> import urllib
>>> urllib.quote("images/PG013001 GROUP 2.jpg")
'images/PG013001%20GROUP%202.jpg'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.