How to download images using urllib 04-07-2013, 01:52 AM
#1
Quick little tutorial on using the urllib module to download images from sites.
Pretty simple stuff. Firstly, we need to import the module (first line). Secondly, we call URLopener() from within our module and use the retrieve() function.
Retrieve takes two parameters, the URL of the image you want to download, and the filename you want to save the image with when it's downloaded to your computer.
Pretty easy stuff hey? You can do a lot with modified versions of this code. I created an Imgur scraper that downloads random images from the site using only a few lines of code. Got to love python. :p
Code:
import urllib
urllib.URLopener().retrieve('http://i.imgur.com/ZHGAYQw.jpg', 'output.jpg')
Pretty simple stuff. Firstly, we need to import the module (first line). Secondly, we call URLopener() from within our module and use the retrieve() function.
Retrieve takes two parameters, the URL of the image you want to download, and the filename you want to save the image with when it's downloaded to your computer.
Pretty easy stuff hey? You can do a lot with modified versions of this code. I created an Imgur scraper that downloads random images from the site using only a few lines of code. Got to love python. :p