How do I download a file over HTTP using Python?

前端 未结 25 2988
感动是毒
感动是毒 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:54

    import urllib2
    mp3file = urllib2.urlopen("http://www.example.com/songs/mp3.mp3")
    with open('test.mp3','wb') as output:
      output.write(mp3file.read())
    

    The wb in open('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.

提交回复
热议问题