使用带有 wget -c 功能的 Python 中的 urllib 下载文件 [英] Download file using urllib in Python with the wget -c feature

查看:46
本文介绍了使用带有 wget -c 功能的 Python 中的 urllib 下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Python 编写一个软件,以便从数据库下载 HTTP PDF.有时下载会因此消息而停止:

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

如何使用 206 Partial Content HTTP 功能要求下载在停止的地方重新启动?

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

我可以使用 wget -c 来实现,而且效果很好,但我想直接在我的 Python 软件中实现它.

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

有什么想法吗?

谢谢

推荐答案

您可以通过发送带有 Range 标头的 GET 请求部分下载:

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>
</body>
</html>






'

请注意检查 Content-Range 以了解实际下载了哪些字节,因为您的范围可能超出范围,和/或并非所有服务器似乎都遵守 Range 标题.

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.

这篇关于使用带有 wget -c 功能的 Python 中的 urllib 下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆