How do I download a file over HTTP using Python?

前端 未结 25 3051
感动是毒
感动是毒 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条回答
  •  梦毁少年i
    2020-11-21 07:56

    Python 3

    • urllib.request.urlopen

      import urllib.request
      response = urllib.request.urlopen('http://www.example.com/')
      html = response.read()
      
    • urllib.request.urlretrieve

      import urllib.request
      urllib.request.urlretrieve('http://www.example.com/songs/mp3.mp3', 'mp3.mp3')
      

      Note: According to the documentation, urllib.request.urlretrieve is a "legacy interface" and "might become deprecated in the future" (thanks gerrit)

    Python 2

    • urllib2.urlopen (thanks Corey)

      import urllib2
      response = urllib2.urlopen('http://www.example.com/')
      html = response.read()
      
    • urllib.urlretrieve (thanks PabloG)

      import urllib
      urllib.urlretrieve('http://www.example.com/songs/mp3.mp3', 'mp3.mp3')
      

提交回复
热议问题