The urllib2 module can be used to download data from the web (network resource access). This data can be a file, a website or whatever you want Python to download. The module supports HTTP, HTTPS, FTP and several other protocols.
In this article you will learn how to download data from the web using Python.Related course
To download a plain text file use this code:
We get a response object using the urllib2.urlopen() method, where the parameter is the link. All of the file contents is received using the response.read() method call. After calling this, we have the file data in a Python variable of type string.
This will request the html code from a website. It will output everything to the screen.
Download file using Python
You can save the data to disk very easily after downloading the file:
The first part of the code downloads the file contents into the variable data:
The second part stores it into a file (this file does not need to have the same filename)
The ‘w’ parameter creates the file (or overwrites if it exists). You can read more about writing files here.Related course