Python (3.5) - urllib.request.urlopen - 进度条可用吗? [英] Python (3.5) - urllib.request.urlopen - Progress Bar Available?

查看:60
本文介绍了Python (3.5) - urllib.request.urlopen - 进度条可用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在万维网上搜索这个答案,但我觉得答案可能是否定的.我使用 Python 3.5 和一个名为 urllib.request 的库和一个名为 urllib.request.urlopen(url) 的方法来打开一个链接并下载一个文件.

I'm trying to search the world wide web for this answer, but I feel there answer may be no. I'm using Python 3.5 and a library called urllib.request with a method called urllib.request.urlopen(url) to open a link and download a file.

最好有某种进度衡量标准,因为文件超过 200MB.我正在查看 API 此处,并且看不到任何类型的带有钩子的参数.

It would be nice to have some kind of measure of progress for this, as the file(s) are over 200MB. I'm looking at the API here, and don't see any kind of parameter with a hook.

这是我的代码:

downloadURL = results[3] #got this from code earlier up
rel_path = account + '/' + eventID + '_' + title + '.mp4'

filename_abs_path = os.path.join(script_dir, rel_path)
print('>>> Downloading >>> ' + title)

# Download .mp4 from a url and save it locally under `file_name`:
with urllib.request.urlopen(downloadURL) as response, open(filename_abs_path, 'wb') as out_file:
    shutil.copyfileobj(response, out_file)

如果有人认为我可能有一个进度条,或者唯一的方法是使用不同的库,那么任何人都可以提供见解吗?我希望保持代码非常简短和简单,我只需要一些正在下载的文件的指示.感谢您提供的任何帮助!

Can anyone provide insight if they think I can potentially have a progress bar or would the only way be to use a different library? I'm looking to keep the code quite short and simple, I just need some indication of the file being downloaded. Thanks for any help provided!

推荐答案

如果响应包含 content-length,您可以读取块中的传入数据并计算完成百分比.不幸的是,分块"响应的 Web 服务器并不总是提供内容长度,因此它并不总是有效.这是一个测试示例.

If the response includes a content-length you can read the incoming data in blocks and calculate percent done. Unfortunately, web servers that "chunk" responses don't always provide a content length, so it doesn't always work. Here is an example to test.

import urllib.request
import sys
import io

try:
    url = sys.argv[1]
except IndexError:
    print("usage: test.py url")
    exit(2)

resp = urllib.request.urlopen(url)
length = resp.getheader('content-length')
if length:
    length = int(length)
    blocksize = max(4096, length//100)
else:
    blocksize = 1000000 # just made something up

print(length, blocksize)

buf = io.BytesIO()
size = 0
while True:
    buf1 = resp.read(blocksize)
    if not buf1:
        break
    buf.write(buf1)
    size += len(buf1)
    if length:
        print('{:.2f}\r done'.format(size/length), end='')
print()

这篇关于Python (3.5) - urllib.request.urlopen - 进度条可用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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