簡體   English   中英

使用wget -c功能在Python中使用urllib下載文件

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

我正在使用Python編程軟件以從數據庫下載HTTP PDF。 有時下載會停止並顯示以下消息:

retrieval incomplete: got only 3617232 out of 10689634 bytes

如何使用206 Partial Content HTTP功能要求下載重新啟動?

我可以使用wget -c來做它並且它工作得很好,但我想直接在我的Python軟件中實現它。

任何的想法 ?

謝謝

您可以通過發送帶有Range標頭的GET來請求部分下載:

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'

請注意檢查Content-Range以了解實際下載的字節數,因為您的范圍可能超出范圍,並且/或者並非所有服務器都看起來都遵循Range標頭。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM