How do I download a file over HTTP using Python?

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

    One more, using urlretrieve:

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

    (for Python 3+ use import urllib.request and urllib.request.urlretrieve)

    Yet another one, with a "progressbar"

    import urllib2
    
    url = "http://download.thinkbroadband.com/10MB.zip"
    
    file_name = url.split('/')[-1]
    u = urllib2.urlopen(url)
    f = open(file_name, 'wb')
    meta = u.info()
    file_size = int(meta.getheaders("Content-Length")[0])
    print "Downloading: %s Bytes: %s" % (file_name, file_size)
    
    file_size_dl = 0
    block_sz = 8192
    while True:
        buffer = u.read(block_sz)
        if not buffer:
            break
    
        file_size_dl += len(buffer)
        f.write(buffer)
        status = r"%10d  [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
        status = status + chr(8)*(len(status)+1)
        print status,
    
    f.close()
    
    0 讨论(0)
  • 2020-11-21 07:46
    import os,requests
    def download(url):
        get_response = requests.get(url,stream=True)
        file_name  = url.split("/")[-1]
        with open(file_name, 'wb') as f:
            for chunk in get_response.iter_content(chunk_size=1024):
                if chunk: # filter out keep-alive new chunks
                    f.write(chunk)
    
    
    download("https://example.com/example.jpg")
    
    0 讨论(0)
  • 2020-11-21 07:46

    This may be a little late, But I saw pabloG's code and couldn't help adding a os.system('cls') to make it look AWESOME! Check it out :

        import urllib2,os
    
        url = "http://download.thinkbroadband.com/10MB.zip"
    
        file_name = url.split('/')[-1]
        u = urllib2.urlopen(url)
        f = open(file_name, 'wb')
        meta = u.info()
        file_size = int(meta.getheaders("Content-Length")[0])
        print "Downloading: %s Bytes: %s" % (file_name, file_size)
        os.system('cls')
        file_size_dl = 0
        block_sz = 8192
        while True:
            buffer = u.read(block_sz)
            if not buffer:
                break
    
            file_size_dl += len(buffer)
            f.write(buffer)
            status = r"%10d  [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
            status = status + chr(8)*(len(status)+1)
            print status,
    
        f.close()
    

    If running in an environment other than Windows, you will have to use something other then 'cls'. In MAC OS X and Linux it should be 'clear'.

    0 讨论(0)
  • 2020-11-21 07:49

    Simple yet Python 2 & Python 3 compatible way comes with six library:

    from six.moves import urllib
    urllib.request.urlretrieve("http://www.example.com/songs/mp3.mp3", "mp3.mp3")
    
    0 讨论(0)
  • 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()
    
    0 讨论(0)
  • 2020-11-21 07:49

    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

    0 讨论(0)
提交回复
热议问题