kognise
kognise

Reputation: 632

Urllib caching image unwantedly?

I am writing a simple script that is supposed to download N random images from Unsplash. It works-- but only mostly. It always downloads N of the same images. I believe urllib is caching the image, but even after trying urllib.urlcleanup() it still downloads the same N images. Can you please help me? Here is my code:

import urllib

num = 4
for i in range(1, num + 1):
    print("Downloading image #" + str(i) + "...")
    urllib.urlretrieve("https://source.unsplash.com/random", "image" + str(i) + ".jpg")
    urllib.urlcleanup()

EDIT: Some people pointed out that I was getting the 404 page. Yes, I was, but after I just fixed that problem the main problem is still occurring.

Upvotes: 0

Views: 82

Answers (2)

yausername
yausername

Reputation: 760

replace

urllib.urlretrieve("https://source.unsplash.com/random" + str(i), "image" + str(i) + ".jpg")

with

urllib.urlretrieve("https://source.unsplash.com/random", "image" + str(i) + ".jpg")

Upvotes: 1

holdenweb
holdenweb

Reputation: 37103

Take a look at the images you actually get. They are all the same because this appears to be the Unsplash 404 page, indicating that Unsplash doesn't recognise it as identifying an image. The URL you are using for random images is probably incorrect.

You may find this page on downloading random images helpful.

Upvotes: 1

Related Questions