August 6, 2008

Python - Downloading Binary Files

Here is a quick example showing how to download and save a binary file (image, zip, etc) to disk using HTTP. This will grab the image file from a website and save it locally:
import urllib

url = 'http://www.goldb.org/images/goldb.png'
f = urllib.urlopen(url)    
fh = open('goldb.png', 'wb')
fh.write(f.read())
fh.close()

1 comment:

Marius Gedminas said...

Shorter version:

import urllib
url = '...'
urllib.urlretrieve(url, 'goldb.png')