- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用您向其发送请求的 API 处理一个小项目,然后它会返回一个带有 zip 文件的响应,然后您可以下载该文件。我第一次自动下载时尝试使用 setopt(curl.WRITEDATA, fp) 函数,但每次尝试都会使我的 Python 脚本崩溃。然后我改变了策略并使用 WRITEFUNCTION 将数据写入缓冲区,然后将其写入一个始终正常工作的文件。
这一切都很好,但后来我想添加一个进度条来查看下载了多少文件并提供一些用户反馈等。这就是事情开始变得奇怪的地方,因为现在进度条在一秒钟内达到 100%并且 zip 文件尚未完成下载。当我将进度函数更改为仅打印正在下载的文件的大小时,它会报告大约 100 个字节的数量(比 zip 文件小得多)。无论如何使用pycurl(和下面的curl)中的函数来跟踪附件下载的进度而不是请求本身?
此外,如果有人可以帮助解决也可能有帮助的 WRITEDATA 问题,我想这两个问题可能有关。
最佳答案
以下代码将使用 pycurl
下载文件并显示当前进度(以文本形式):
import pycurl
# for displaying the output text
from sys import stderr as STREAM
# replace with your own url and path variables
url = "http://ovh.net/files/100Mb.dat"
path = 'test_file.dat'
# use kiB's
kb = 1024
# callback function for c.XFERINFOFUNCTION
def status(download_t, download_d, upload_t, upload_d):
STREAM.write('Downloading: {}/{} kiB ({}%)\r'.format(
str(int(download_d/kb)),
str(int(download_t/kb)),
str(int(download_d/download_t*100) if download_t > 0 else 0)
))
STREAM.flush()
# download file using pycurl
with open(path, 'wb') as f:
c = pycurl.Curl()
c.setopt(c.URL, url)
c.setopt(c.WRITEDATA, f)
# display progress
c.setopt(c.NOPROGRESS, False)
c.setopt(c.XFERINFOFUNCTION, status)
c.perform()
c.close()
# keeps progress onscreen after download completes
print()
输出应如下所示:
Downloading: 43563/122070 kiB (35%)
tqdm
包生成进度条。它会在文件下载时实时更新,并显示下载速度和估计剩余时间。由于方式限制
tqdm
作品,
requests
还需要包。这也与
total_dl_d
的原因有关。变量是一个数组而不是一个整数。
import pycurl
# needed to predict total file size
import requests
# progress bar
from tqdm import tqdm
# replace with your own url and path variables
url = "http://ovh.net/files/10Mb.dat"
path = 'test_file.dat'
# show progress % and amount in bytes
r = requests.get(url)
total_size = int(r.headers.get('content-length', 0))
block_size = 1024
# create a progress bar and update it manually
with tqdm(total=total_size, unit='iB', unit_scale=True) as pbar:
# store dotal dl's in an array (arrays work by reference)
total_dl_d = [0]
def status(download_t, download_d, upload_t, upload_d, total=total_dl_d):
# increment the progress bar
pbar.update(download_d - total[0])
# update the total dl'd amount
total[0] = download_d
# download file using pycurl
with open(path, 'wb') as f:
c = pycurl.Curl()
c.setopt(c.URL, url)
c.setopt(c.WRITEDATA, f)
# follow redirects:
c.setopt(c.FOLLOWLOCATION, True)
# custom progress bar
c.setopt(c.NOPROGRESS, False)
c.setopt(c.XFERINFOFUNCTION, status)
c.perform()
c.close()
fp
即 file_path
)...
WRITEDATA
) 问题可能是由于提供了文件路径 (str) 而不是文件对象 (io.BufferedWriter)。
XFERINFOFUNCTION
文件下载期间会重复调用回调。回调仅提供总文件大小和已下载的总数作为参数。它不计算自上次调用以来的增量(差值)。使用进度条描述的问题(“进度条在一秒钟内达到 100%,而 zip 文件尚未完成下载”)可能是由于(下载的)总量被用作
update
预期增加金额时的金额。
如果进度条每次都增加总量,那么它不会反射(reflect)实际下载的数量。 它将显示更大的数量。然后,它将超过100%并出现各种故障。
关于python - PycURL 附件和进度函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19724222/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!