Python Download File from URL [with 3 Examples] https://www.codingem.
com/python-download-file-from-url/
c
o
di
n
g
e
m
.c
o
m
Python How to Download a File from a URL
By Artturi Jalli
To download a file from a URL using Python, use the requests.get() method. For
example, let’s download Instagram’s icon:
import requests
URL = "https://instagram.com/favicon.ico"
response = requests.get(URL)
open("instagram.ico", "wb").write(response.content)
This is an example for someone who is looking for a quick answer. But if the above
code lines don’t work or make sense, please keep on reading.
More Detailed Steps
To download a file from a URL using Python follow these three steps:
x
1. Install requests module and import it to your project.
2. Use requests.get() to download the data behind that URL.
1 of 14 12/1/2022, 11:22 AM
Python Download File from URL [with 3 Examples] https://www.codingem.com/python-download-file-from-url/
3. Write the file to a file in your system by calling open().
Here is an example:
Let’s download Instagram’s icon using Python. The icon can be found behind this
URL https://instagram.com/favicon.ico.
First, install the requests module by opening up a command line window and running:
pip install requests
Then, you can use it to download the icon behind the URL:
# 1. Import the requests library
import requests
URL = "https://instagram.com/favicon.ico"
# 2. download the data behind the URL
response = requests.get(URL)
# 3. Open the response into a new file called instagram.ico
open("instagram.ico", "wb").write(response.content)
As a result of running this piece of code, you see the Instagram icon appear in the
same folder where your program file is.
Other Ways to Download a File in Python
There are other modules that make downloading files possible in Python.
2 of 14 12/1/2022, 11:22 AM
Python Download File from URL [with 3 Examples] https://www.codingem.com/python-download-file-from-url/
In addition to the requests library, the two commonly used ones are:
• wget
• urllib
How to Download a File Using wget Module
Before you can download files using wget, you need to install the wget module.
Open up a command line window and run:
pip install wget
Then follow these two steps to download a file:
1. Import the wget module into your project.
2. Use wget.download() to download a file from a specific URL and save it on
your machine.
As an example, let’s get the Instagram icon using wget:
import wget
URL = "https://instagram.com/favicon.ico"
response = wget.download(URL, "instagram.ico")
x
As a result of running the code, you can see an Instagram icon appear in the folder of
your program.
3 of 14 12/1/2022, 11:22 AM
Python Download File from URL [with 3 Examples] https://www.codingem.com/python-download-file-from-url/
How to Download a File Using urllib Module in Python
Before you can download files using urllib, you need to install the module. Open up
a command line window and run:
pip install urllib
Then follow these two steps to download a file:
1. Import the urllib module into your project.
2. Use urllib‘s request.urlretrieve() method to download a file from a specific
URL and save it on your machine.
As an example, let’s get the Instagram icon using urllib:
from urllib import request
URL = "https://instagram.com/favicon.ico"
response = request.urlretrieve("https://instagram.com/favicon.ico", x
As a result of running the code, you can see an Instagram icon appear in the folder of
4 of 14 12/1/2022, 11:22 AM
Python Download File from URL [with 3 Examples] https://www.codingem.com/python-download-file-from-url/
your program.
Conclusion
Thanks for reading.
Happy coding!
Further Reading
How to Write to a File in Python
How to Read a File in Python
← Previous Post Next Post →
Leave a Comment x
Your email address will not be published. Required fields are marked *
5 of 14 12/1/2022, 11:22 AM
Python Download File from URL [with 3 Examples] https://www.codingem.com/python-download-file-from-url/
Type here..
Name* E-mail*
Website
Post Comment »
report this ad
6 of 14 12/1/2022, 11:22 AM
Python Download File from URL [with 3 Examples] https://www.codingem.com/python-download-file-from-url/
report this ad
Search …
7 of 14 12/1/2022, 11:22 AM
Python Download File from URL [with 3 Examples] https://www.codingem.com/python-download-file-from-url/
About the Author
Hi, I'm Artturi Jalli!
I'm a Tech enthusiast from Finland.
I make Coding & Tech easy and fun with well-thought how-to guides and
reviews.
I've already helped 2M+ visitors reach their goals!
Contact
8 of 14 12/1/2022, 11:22 AM
Python Download File from URL [with 3 Examples] https://www.codingem.com/python-download-file-from-url/
13 Best AI Art Generators of 2022 (Free &
Paid)
Choosing the right type of AI art generator is crucial to produce unique, original,
and professional artwork. With the latest advancements in AI art generation, you
can...
Continue Reading
Newor Media Review: Is It the Best
AdSense Alternative?
9 of 14 12/1/2022, 11:22 AM
Python Download File from URL [with 3 Examples] https://www.codingem.com/python-download-file-from-url/
AdSense Alternative?
Image Credit: Newor Media To turn yourself into a full-time blogger, you have to
be good at monetizing your blog. One of the predominant approaches to monetiz-
ing...
Continue Reading
How to Make an App — A Complete 10-
Step Guide [in 2022]
10 of 14 12/1/2022, 11:22 AM
Python Download File from URL [with 3 Examples] https://www.codingem.com/python-download-file-from-url/
9 Best Graphic Design Courses +
Certification [in 2022]
Are you looking to create the next best-seller app? Or are you curious about how
to create a successful mobile app? This is a step-by-step guide on...
Continue Reading
Do you want to become a versatile and skilled graphic designer? This is a com-
prehensive article on the best graphic design certification courses. These courses
prepare you...
Continue Reading
8 Best Python Courses with
Certifications [in 2022]
11 of 14 12/1/2022, 11:22 AM
Python Download File from URL [with 3 Examples] https://www.codingem.com/python-download-file-from-url/
Are you looking to become a professional Python developer? Or are you inter-
ested in programming but don’t know where to start? Python is a beginner-
friendly and versatile...
Continue Reading
8 Best Swift & iOS App Development
Courses [in 2022]
Are you looking to become an iOS developer? Do you want to create apps with
12 of 14 12/1/2022, 11:22 AM
Python Download File from URL [with 3 Examples] https://www.codingem.com/python-download-file-from-url/
an outstanding design? Do you want to learn to code? IOS App...
Continue Reading
report this ad
Recent Posts
13 Best AI Art Generators of 2022 (Free & Paid)
Git How to Change Remote Origin (with Examples)
10 Best AI Chatbots of 2022 (Ranked & Reviewed)
Difference Between ‘git add . ‘ and ‘git add -A’ (with Examples)
Git How to See the Difference between Two Branches
Categories
Artificial Intelligence
Crypto & NFT
Data Science
Favorites
Git
iOS Development
JavaScript
Linux
x
Programming
Programming Tips
13 of 14 12/1/2022, 11:22 AM
Python Download File from URL [with 3 Examples] https://www.codingem.com/python-download-file-from-url/
Python
Python for Beginners
Software
Swift
Swift for Beginners
Technology
Web development
report this ad
Copyright © 2022 codingem.com | Powered by Astra WordPress Theme
14 of 14 12/1/2022, 11:22 AM