How do I download a file over HTTP using Python?

前端 未结 25 3016
感动是毒
感动是毒 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 08:04

    urlretrieve and requests.get are simple, however the reality not. I have fetched data for couple sites, including text and images, the above two probably solve most of the tasks. but for a more universal solution I suggest the use of urlopen. As it is included in Python 3 standard library, your code could run on any machine that run Python 3 without pre-installing site-package

    import urllib.request
    url_request = urllib.request.Request(url, headers=headers)
    url_connect = urllib.request.urlopen(url_request)
    
    #remember to open file in bytes mode
    with open(filename, 'wb') as f:
        while True:
            buffer = url_connect.read(buffer_size)
            if not buffer: break
    
            #an integer value of size of written data
            data_wrote = f.write(buffer)
    
    #you could probably use with-open-as manner
    url_connect.close()
    

    This answer provides a solution to HTTP 403 Forbidden when downloading file over http using Python. I have tried only requests and urllib modules, the other module may provide something better, but this is the one I used to solve most of the problems.

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