首页 > 知识库 > 生活常识 >

python wget.download()自定义下载样式(1.1-下载 Python)

生活常识 2022-12-12 18:29:11
今天我写 下载的时候,我和我的同事对于下载方式起了争议. 我使用的是wget方法实现的,我的同事使用的是requests...更多python wget.download()自定义下载样式的内容,欢迎关注我们的专题频道。

1.1-下载 Python

python wget.download()自定义下载样式

python wget.download()自定义下载样式(1.1-下载 Python)

今天我写 下载的时候,我和我的同事对于下载方式起了争议.

我使用的是wget方法实现的,我的同事使用的是requests方法实现的.

然后我的同事的requests方式实现的自定义下载样式嘛,是从网上复制粘贴来的。

我就想着:我去网上找一下也许也能实现,后来我发现网上没有,就想着自己实现吧.

 

本次所用到的库:wget库

可以通过cmd下载:

pip install wget

接着上文,然后就开始趴wget库的源代码,发现wget.download()的定义是支持自定义下载样式的

wget.download()的定义:

download(url, out=None, bar=bar_adaptive):

我就想着bar_adaptive是什么东西,

然后看完bar_adaptive的代码就明白了,bar_adaptive是自定义下载进度条样式的

bar_adaptive的定义:

bar_adaptive(current, total, width=80):

那也不多说了,直接上代码展示:(这里只改了进度的显示单位,其实还可以改的)

def abar_adaptive(current, total, width=80): # 这段代码是wget库的bar_adaptive()函数所简单修改来的,这个只改了下载进度条显示下载单位的功能, # process special case when total size is unknown and return immediately if not total or total =1024: current_tell = current_tell/1024 else: current_tell = format(round(current_tell, 2),".2f")+i break msg = "%s / unknown" % current_tell if len(msg) < width: # leaves one character to avoid linefeed return msg if len("%s" % current) < width: return "%s" % current_tell # --- adaptive layout algorithm --- # # [x] describe the format of the progress bar # [x] describe min width for each data field # [x] set priorities for each element # [x] select elements to be shown # [x] choose top priority element min_width < avail_width # [x] lessen avail_width by value if min_width # [x] exclude element from priority list and repeat # 10% [.. ] 10/100 # pppp bbbbb sssssss min_width = { 'percent': 4, # 100% 'bar': 3, # [.] 'size': len("%s" % total)*2 + 3, # 'xxxx / yyyy' } priority = ['percent', 'bar', 'size'] # select elements to show selected = [] avail = width for field in priority: if min_width[field] =1024: current_tell = current_tell/1024 else: current_tell = format(round(current_tell, 2),".2f")+i break for i in ['B','K','MB','GB','TB','PB','EB','ZB','YB','BB','NB','DB']:#单位转换 if total_tell >=1024: total_tell = total_tell/1024 else: total_tell = format(round(total_tell, 2),".2f")+i break output += ("%s / %s" % (current_tell, total_tell)).rjust(min_width['size']) selected = selected[1:] if selected: output += ' ' # add field separator return output

返回的output就是我们所看到的下载进度条,这里只加了一个下载的文件单位显示,

对于我们自定义好的下载样式,就可以在wget.download()里面用了

调用演示:

 这个自定义样式可以做到的不止这么多,当前下载速度什么的都可以搞,今天教程就到此结束了


python wget.download()自定义下载样式拓展阅读

python wget.download()自定义下载样式(1.1-下载 Python)

今天我写 下载的时候,我和我的同事对于下载方式起了争议. 我使用的是wget方法实现的,我的同事使用的是requests方法实现的. 然后我的同事的reques....
以上就是关于python wget.download()自定义下载样式(1.1-下载 Python)的所有内容,希望对你学习有所帮助。

标签: python wget.download()自定义下载样式

【免责声明】本站所有文章(含图片和视频)由网站用户自行上传发布,平台仅提供信息存储服务,并不代表本站立场和观点,若有侵犯你的权利,请及时联系我们删除。
Copyright © 2016-2020 shuguohai.com All Rights Reserved. 皖ICP备2022016496号