Python Download File From URL (With 3 Examples)
Python Download File From URL (With 3 Examples)
com/python-download-file-from-url/
c
o
di
n
g
e
m
.c
o
m
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.
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/
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:
Then, you can use it to download the icon behind the URL:
URL = "https://instagram.com/favicon.ico"
As a result of running this piece of code, you see the Instagram icon appear in the
same folder where your program file is.
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
Before you can download files using wget, you need to install the wget module.
import wget
URL = "https://instagram.com/favicon.ico"
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/
Before you can download files using urllib, you need to install the module. Open up
a command line window and run:
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
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/
I make Coding & Tech easy and fun with well-thought how-to guides and
reviews.
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/
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
AdSense Alternative?
Image Credit: Newor Media To turn yourself into a full-time blogger, you have to
ing...
Continue Reading
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/
Are you looking to create the next best-seller app? Or are you curious about how
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
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-
Continue Reading
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/
Continue Reading
report this ad
Recent Posts
Difference Between ‘git add . ‘ and ‘git add -A’ (with Examples)
Categories
Artificial Intelligence
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
Software
Swift
Technology
Web development
report this ad
14 of 14 12/1/2022, 11:22 AM