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

本文讲述了作者在实现下载功能时,与同事关于下载方式的讨论。作者选择了wget库,而同事使用了requests库。通过研究wget库源码,作者发现可以自定义下载进度条样式,并详细解析了`bar_adaptive`函数,展示了如何修改代码以显示下载单位。最终,作者提供了一段自定义下载进度条样式的代码,并鼓励读者探索更多可能性。
摘要由CSDN通过智能技术生成

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

我使用的是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()里面用了

调用演示:

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

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值