Download file using urllib in Python with the wget -c feature

前端 未结 1 789
借酒劲吻你
借酒劲吻你 2020-12-17 00:47

I am programming a software in Python to download HTTP PDF from a database. Sometimes the download stop with this message :

retrieval incomplete: got only 3         


        
相关标签:
1条回答
  • 2020-12-17 01:25

    You can request a partial download by sending a GET with the Range header:

    import urllib2
    req = urllib2.Request('http://www.python.org/')
    #
    # Here we request that bytes 18000--19000 be downloaded.
    # The range is inclusive, and starts at 0.
    #
    req.headers['Range'] = 'bytes=%s-%s' % (18000, 19000)
    f = urllib2.urlopen(req)
    # This shows you the *actual* bytes that have been downloaded.
    range=f.headers.get('Content-Range')
    print(range)
    # bytes 18000-18030/18031
    print(repr(f.read()))
    # '  </div>\n</body>\n</html>\n\n\n\n\n\n\n'
    

    Be careful to check the Content-Range to learn what bytes have actually been downloaded, since your range may be out of bounds, and/or not all servers seem to respect the Range header.

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