python get 下载 目录_使用wget -c功能在Python中使用urllib下载文件

1586010002-jmsa.png

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 3617232 out of 10689634 bytes

How can I ask the download to restart where it stops using the 206 Partial Content HTTP feature ?

I can do it using wget -c and it works pretty well, but I would like to implement it directly in my Python software.

Any idea ?

Thank you

解决方案

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()))

# '

\n
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值