1162

I have a small utility that I use to download an MP3 file from a website on a schedule and then builds/updates a podcast XML file which I've added to iTunes.

The text processing that creates/updates the XML file is written in Python. However, I use wget inside a Windows .bat file to download the actual MP3 file. I would prefer to have the entire utility written in Python.

I struggled to find a way to actually download the file in Python, thus why I resorted to using wget.

So, how do I download the file using Python?

4
  • 2
    Many of the answers below are not a satisfactory replacement for wget. Among other things, wget (1) preserves timestamps (2) auto-determines filename from url, appending .1 (etc.) if the file already exists (3) has many other options, some of which you may have put in your .wgetrc. If you want any of those, you have to implement them yourself in Python, but it's simpler to just invoke wget from Python. Commented Sep 27, 2016 at 17:22
  • 3
    Short solution for Python 3: import urllib.request; s = urllib.request.urlopen('http://example.com/').read().decode()
    – Basj
    Commented Nov 26, 2019 at 9:47
  • wget is still a better approach, if you need to automatically retrieve filename and timestamps and handling duplicating files as stackoverflow.com/users/4958/shreevatsar stated. If the urls are variables, one can still handle in python using subprocess.
    – Tendai
    Commented Mar 2, 2023 at 8:19

31 Answers 31

1
2
-5

Another way is to call an external process such as curl.exe. Curl by default displays a progress bar, average download speed, time left, and more all formatted neatly in a table. Put curl.exe in the same directory as your script

from subprocess import call
url = ""
call(["curl", {url}, '--output', "song.mp3"])

Note: You cannot specify an output path with curl, so do an os.rename afterwards

1
2

Not the answer you're looking for? Browse other questions tagged or ask your own question.