How do I download a file over HTTP using Python?

前端 未结 25 2987
感动是毒
感动是毒 2020-11-21 07:17

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 te

25条回答
  •  迷失自我
    2020-11-21 07:49

    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:

    import urllib
    response = urllib.urlopen('http://www.example.com/sound.mp3')
    mp3 = response.read()
    

    Will work fine. Or, if you don't want to deal with the "response" object you can call read() directly:

    import urllib
    mp3 = urllib.urlopen('http://www.example.com/sound.mp3').read()
    

提交回复
热议问题