Saqib_
Saqib_

Reputation: 3

How to make python download an image from a URL but if the image is already downloaded it doesnt

So say from a random api, lets say api.example.com as an example. It sends a random image once you go on it and sends the json for it. So like {"url": "api.example.com/img1.png"}. After de-jsonifying it how can i download the image and save it in some folder, but if its already downloaded so say the image name is taken it will not download it.

Edit: here is my code i done so far.

`

url = f"https://nekos.life/api/v2/img/neko"
response = requests.get(url)
response.raise_for_status()
jsonResponse = response.`json()
urll = (jsonResponse["url"])
urllib.request.urlretrieve(urll, "neko.png")`

Upvotes: 0

Views: 66

Answers (1)

Michele Ceccacci
Michele Ceccacci

Reputation: 26

as said in this article, i think [os.path][1] can do the job pretty well. just try to use

os.path.exists(phot_path)

that should be it. [1]: https://linuxize.com/post/python-check-if-file-exists/

Upvotes: 1

Related Questions