栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

我使用的是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 < 0:
        current_tell = current
        for i in ['B','K','MB','GB','TB','PB','EB','ZB','YB','BB','NB','DB']:#单位转换
            if current_tell >=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] < avail:
        selected.append(field)
        avail -= min_width[field]+1   # +1 is for separator or for reserved space at
                                      # the end of line to avoid linefeed on Windows
    # render
    output = ''
    for field in selected:

      if field == 'percent':
        # fixed size width for percentage
        output += ('%s%%' % (100 * current // total)).rjust(min_width['percent'])
      elif field == 'bar':  # [. ]
        # bar takes its min width + all available space
        #这里需要注意一下,这段代码原来是wget库里面的,因此下面的部分也要加上,当然下面的可以去掉,它的作用只是显示[...]这样子的进度条
        output += wget.bar_thermometer(current, total, min_width['bar']+avail)
      elif field == 'size':
        # size field has a constant width (min == max)
        # 显示实际的单位,默认值的单位是B
        # 下面的单位转换是笔者自己加的
        current_tell,total_tell = current,total
        for i in ['B','K','MB','GB','TB','PB','EB','ZB','YB','BB','NB','DB']:#单位转换
            if current_tell >=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()里面用了

调用演示:

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/857095.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号