We can download data using the urllib2 module.. These examples work with both http, https and for any type of files including text and image.
Data will be saved in the same directory as your program resides. This means if you run your program from C:\apps\, images will be saved there too unless you explicitly specify another directory.
Download data to variable
To download data to a variable, you can use this code:
The first line, ‘import urllib2’, loads the module. The second line opens the connection to the url. The method response.read() downloads data from the url and stores it into the variable data.
Download text file
To download a file you can use this code:
Download image file
Downloading an image from the web works in the same way: