Python provides several ways to download files from the internet. This can be done over HTTP using the urllib package or the requests library. This tutorial will discuss how to use these libraries to download files from URLs using Python.
REQUESTS
The requests library is one of the most popular libraries in Python. Requests allow you to send HTTP/1.1 requests without the need to manually add query strings to your URLs, or form-encode your POST data.
With the requests library, you can perform a lot of functions including:
import urllib.request
with urllib.request.urlopen('http://python.org/') as response:
html = response.read()
If you wish to retrieve an internet resource and store it, you can do so via the urlretrieve() function.
import urllib.request
filename, headers = urllib.request.urlretrieve('http://python.org/')
html = open(filename)
Downloading Images With Python
In this example, we want to download the image available on this link using both the request llibrary and urllib module.
url = 'https://www.python.org/static/opengraph-icon-200x200.png'
# downloading with urllib
# imported the urllib library
import urllib
# Copy a network object to a local file
urllib.urlretrieve(url, "python.png")
# downloading with requests
# import the requests library
import requests
# download the url contents in binary format
r = requests.get(url)
# open method to open a file on your system and write the contents
with open("python1.png", "wb") as code:
code.write(r.content)
Download PDF Files With Python
In this example, we will download a pdf about google trends from this link.
url = 'https://static.googleusercontent.com/media/www.google.com/en//googleblogs/pdfs/google_predicting_the_present.pdf'
# downloading with urllib
# import the urllib package
import urllib
# Copy a network object to a local file
urllib.urlretrieve(url, "tutorial.pdf")
# downloading with requests
# import the requests library
import requests
# download the file contents in binary format
r = requests.get(url)
# open method to open a file on your system and write the contents
with open("tutorial1.pdf", "wb") as code:
code.write(r.content)
Download Zip Files With Python
In this example, we are going to download the contents of a GitHub repository found in this link and store the file locally.
url = 'https://codeload.github.com/fogleman/Minecraft/zip/master'
# downloading with requests
# import the requests library
import requests
# download the file contents in binary format
r = requests.get(url)
# open method to open a file on your system and write the contents
with open("minemaster1.zip", "wb") as code:
code.write(r.content)
# downloading with urllib
# import the urllib library
import urllib
# Copy a network object to a local file
urllib.urlretrieve(url, "minemaster.zip")
Download Videos With Python
In this example, we want to download the video lecture available on this page
url = 'https://www.youtube.com/watch?v=aDwCCUfNFug'
video_name = url.split('/')[-1]
# using requests
# imported the requests library
import requests
print "Downloading file:%s" % video_name
# download the url contents in binary format
r = requests.get(url)
# open method to open a file on your system and write the contents
with open('tutorial.mp4', 'wb') as f:
f.write(r.content)
# using urllib
# imported the urllib library
import urllib
print "Downloading file:%s" % video_name
# Copy a network object to a local file
urllib.urlretrieve(url, "tutorial2.mp4")
Conclusion
This tutorial has covered the most commonly used methods to download files as well as the most common file formats. Even though you will write less code when using the urllib module, the requests module is preferred due to its simplicity, popularity and a wide array of features including:
This website uses cookies to improve your experience. AcceptRead More
Privacy & Cookies Policy
Privacy Overview
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
Create a free account, or log in.
Gain access to read this article, join developers who build better, faster, smarter solutions
Upgrade your subscription
Get full access to all content. Join developers who build better, faster, smarter solutions