I know that there is a lot of questions similar questions, but I think, that there is not an igual (I think). I have an array of image's urls and I want to download it. But when I try to save images I get this error. I don't know how to make it works.
This is my code:
listOfImagesUrl = ['https://cdn.psychologytoday.com/sites/default/files/blogs/1023/2012/09/105928-103553.jpg', 'http://i.livescience.com/images/i/000/048/264/original/disgusted-101130-02.jpg%3F1324346664', 'http://barfblog.com/wp-content/uploads/images/disgust.story.jpg', 'http://cache1.asset-cache.net/gc/148190074-people-making-disgusted-faces-gettyimages.jpg%3Fv%3D1%26c%3DIWSAsset%26k%3D2%26d%3Dww%252BvNwEe%252BXzLnQze1Z2w9KNDivKR%252BEqGJ2cPfDe1oeinIezLX%252B8y1tIG3LNjTbL5']
imageNumber = 1
for imageUrl in listOfImagesUrl:
file = cStringIO.StringIO(urllib.urlopen(imageUrl).read())
img = Image.open(file)
img.save("/tmp/test/" + str(imageNumber) + "." + img.format)
print "DONE: " + str(imageNumber) + " of " + str(len(listOfImagesUrl))
imageNumber += 1
I solved the url problem using the sleeplessnerd's answer on this stackoverflow's question. The problem was that I had to enable cookies on urllib2.
urllib
and the response body is empty. Do check your responses before trying to read data. – Martijn Pieters♦ Aug 24 '15 at 8:07%3F
sequence is an encoded question mark. It should not be part of the URL path, you have a query parameter (cache buster probably), encoded into the URL instead. The last URL has the same. – Martijn Pieters♦ Aug 24 '15 at 8:08urllib.urlopen
documentation says "Deprecated since version 2.6: Theurlopen()
function has been removed in Python 3 in favor ofurllib2.urlopen()
." – martineau Aug 24 '15 at 8:36