Home > database >  Python - read array and download each image locally
Python - read array and download each image locally

Time:03-07

my favourite helpful people.

Im trying to download openweathermap weather icons locally, and trying to find a way of recursively saving them.

I have this code to save individual ones, but would like to be able to try a for each loop to run through a list/array to get all the others. Basic stuff I know, just having a brain freeze right now.

This is the single download code:

import urllib.request
imgURL = "http://openweathermap.org/img/w/03d.png"

urllib.request.urlretrieve(imgURL, "weathericons/03d.png")

03d.png is one of the files I would like to iterate through an array or list to get the other images, so would like to download based on changing the last part of the URL with each of the image names.

Hope someone can help, many thanks

CodePudding user response:

No worries peeps...I just downloaded them manually as there werent that many.

Just need to convert them to JPG now :)

CodePudding user response:

You can use a for loop with some format strings to generate the desired URLs:

import urllib.request

for i in range(1, 5):
     imgURL = f"http://openweathermap.org/img/w/0{i}d.png"
     urllib.request.urlretrieve(imgURL, f"weathericons/0{i}d.png")
  • Related