Isn’t it easy to download the file using Python 3 and that file will be at your folder directory where you can easily read, check, and enjoy it? Yes, it is also possible in Python which pretty easy, simple to do.
Contents
How to Download Files Using Python 3?
What would be if it is possible?
There are simple ways such as making with function, without function, Tkinter, input type just paste out the URL and download it, and more.
Have you already installed the requests library of Python? If yet not, then follow the below-mentioned command.
$ pip install requests
In a first way, we would like to have how to do with function?
Def function to download files
Its pretty simple to do: let’s begin!
from requests import get # to make GET request def download(url, file_name): # open, download in binary mode with open(file_name, "wb") as file: # get request response = get(url) # write to file file.write(response.content)
Now run the function with the following code:
download(https://wholeblogs.com/wp-content/uploads/2021/03/pure-white-backg.png, "wholeblogs")
You can also use the above code without using the function.
Download file without function
Here you go:
import requests image_url = "https://wholeblogs.com/wp-content/uploads/2021/03/pure-white-backg.png" # URL of the image to be downloaded is defined as image_url r = requests.get(image_url) # create HTTP response object # send a HTTP request to the server and save # the HTTP response in a response object called r with open("wholeblog.png",'wb') as f: # Saving received content as a png file in # binary format # write the contents of the response (r.content) # to a new file in binary mode. f.write(r.content)
Go and check out where you are using this file there is downloaded a new image that name is: “wholeblog.png” and it will be there before running the code.
Read more: How To Install difflib in Python Using CMD?
Download files using wget library Python
In this library pretty easy to download the PNG, JPG, JPEG, GIF. I would recommend you to see more idea: here
Here we go:
import wget url = 'https://wholeblogs.com/wp-content/uploads/2021/03/pure-white-backg.png' wget.download(url, '/Users/scott/Downloads/wholeblog.jpg')
It will be placed in your users > name of computer > downloads then there will have a new pic that name “wholeblog.jpg” you can also replace with “.jpg” to “.png” but according to the image the process will be.
Download files using urllib.request Python library
It is also the same but according to the ways, I like to share whatever I have in my folders and whatever I did study.
import urllib.request url = 'https://wholeblogs.com/wp-content/uploads/2021/03/pure-white-backg.png' urllib.request.urlretrieve(url, "wholeblogs.png")
That’s it.
You can read more: How to Check if an Item Exists in List Using Python?