当前位置:   article > 正文

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

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):

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

  1. def abar_adaptive(current, total, width=80):
  2. # 这段代码是wget库的bar_adaptive()函数所简单修改来的,这个只改了下载进度条显示下载单位的功能,
  3. # process special case when total size is unknown and return immediately
  4. if not total or total < 0:
  5. current_tell = current
  6. for i in ['B','K','MB','GB','TB','PB','EB','ZB','YB','BB','NB','DB']:#单位转换
  7. if current_tell >=1024:
  8. current_tell = current_tell/1024
  9. else:
  10. current_tell = format(round(current_tell, 2),".2f")+i
  11. break
  12. msg = "%s / unknown" % current_tell
  13. if len(msg) < width: # leaves one character to avoid linefeed
  14. return msg
  15. if len("%s" % current) < width:
  16. return "%s" % current_tell
  17. # --- adaptive layout algorithm ---
  18. #
  19. # [x] describe the format of the progress bar
  20. # [x] describe min width for each data field
  21. # [x] set priorities for each element
  22. # [x] select elements to be shown
  23. # [x] choose top priority element min_width < avail_width
  24. # [x] lessen avail_width by value if min_width
  25. # [x] exclude element from priority list and repeat
  26. # 10% [.. ] 10/100
  27. # pppp bbbbb sssssss
  28. min_width = {
  29. 'percent': 4, # 100%
  30. 'bar': 3, # [.]
  31. 'size': len("%s" % total)*2 + 3, # 'xxxx / yyyy'
  32. }
  33. priority = ['percent', 'bar', 'size']
  34. # select elements to show
  35. selected = []
  36. avail = width
  37. for field in priority:
  38. if min_width[field] < avail:
  39. selected.append(field)
  40. avail -= min_width[field]+1 # +1 is for separator or for reserved space at
  41. # the end of line to avoid linefeed on Windows
  42. # render
  43. output = ''
  44. for field in selected:
  45. if field == 'percent':
  46. # fixed size width for percentage
  47. output += ('%s%%' % (100 * current // total)).rjust(min_width['percent'])
  48. elif field == 'bar': # [. ]
  49. # bar takes its min width + all available space
  50. #这里需要注意一下,这段代码原来是wget库里面的,因此下面的部分也要加上,当然下面的可以去掉,它的作用只是显示[...]这样子的进度条
  51. output += wget.bar_thermometer(current, total, min_width['bar']+avail)
  52. elif field == 'size':
  53. # size field has a constant width (min == max)
  54. # 显示实际的单位,默认值的单位是B
  55. # 下面的单位转换是笔者自己加的
  56. current_tell,total_tell = current,total
  57. for i in ['B','K','MB','GB','TB','PB','EB','ZB','YB','BB','NB','DB']:#单位转换
  58. if current_tell >=1024:
  59. current_tell = current_tell/1024
  60. else:
  61. current_tell = format(round(current_tell, 2),".2f")+i
  62. break
  63. for i in ['B','K','MB','GB','TB','PB','EB','ZB','YB','BB','NB','DB']:#单位转换
  64. if total_tell >=1024:
  65. total_tell = total_tell/1024
  66. else:
  67. total_tell = format(round(total_tell, 2),".2f")+i
  68. break
  69. output += ("%s / %s" % (current_tell, total_tell)).rjust(min_width['size'])
  70. selected = selected[1:]
  71. if selected:
  72. output += ' ' # add field separator
  73. return output

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

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

调用演示:

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/316727
推荐阅读
相关标签
  

闽ICP备14008679号