I have a small utility that I use to download a MP3 from a website on a schedule and then builds/updates a podcast XML file which I've obviously added to iTunes.
The text processing that creates/updates the XML file is written in Python. I use wget inside a Windows .bat
file to download the actual MP3 however. I would prefer to have the entire utility written in Python though.
I struggled though to find a way to actually down load the file in Python, thus why I resorted to wget
.
So, how do I download the file using Python?
One more, using
urlretrieve
:(for Python 3+ use 'import urllib.request' and urllib.request.urlretrieve)
Yet another one, with a "progressbar"
The
wb
inopen('test.mp3','wb')
opens a file (and erases any existing file) in binary mode so you can save data with it instead of just text.I agree with Corey, urllib2 is more complete than urllib and should likely be the module used if you want to do more complex things, but to make the answers more complete, urllib is a simpler module if you want just the basics:
Will work fine. Or, if you don't want to deal with the "response" object you can call read() directly:
use wget module:
Wrote wget library in pure Python just for this purpose. It is pumped up
urlretrieve
with these features as of version 2.0.In python3 you can use urllib3 and shutil libraires. Download them by using pip or pip3 (Depending whether python3 is default or not)
Then run this code
Note that you download
urllib3
but useurllib
in code