Python HTTP下载文件并显示下载进度条功能的实现


Posted in Python onApril 02, 2020

下面的Python脚本中利用request下载文件并写入到文件系统,利用progressbar模块显示下载进度条。

其中利用request模块下载文件可以直接下载,不需要使用open方法,例如:

import urllib
import requests.packages.urllib3

requests.packages.urllib3.disable_warnings()

url = "https://raw.githubusercontent.com/racaljk/hosts/master/hosts"
urllib.urlretrieve(url, filename="hosts")

下面的例子是题目中完整的例子,其中注释的部分是进度条的另一种写法,显示当前处理过的行数。

#!/usr/bin/python
# encoding: utf-8
# -*- coding: utf8 -*-
"""
Created by PyCharm.
File:        LinuxBashShellScriptForOps:download_file2.py
User:        Guodong
Create Date:    2016/9/14
Create Time:    9:40
 """
import requests
import progressbar
import requests.packages.urllib3

requests.packages.urllib3.disable_warnings()

url = "https://raw.githubusercontent.com/racaljk/hosts/master/hosts"

response = requests.request("GET", url, stream=True, data=None, headers=None)

save_path = "/tmp/hosts"

total_length = int(response.headers.get("Content-Length"))
with open(save_path, 'wb') as f:
  # widgets = ['Processed: ', progressbar.Counter(), ' lines (', progressbar.Timer(), ')']
  # pbar = progressbar.ProgressBar(widgets=widgets)
  # for chunk in pbar((i for i in response.iter_content(chunk_size=1))):
  #   if chunk:
  #     f.write(chunk)
  #     f.flush()

  widgets = ['Progress: ', progressbar.Percentage(), ' ',
        progressbar.Bar(marker='#', left='[', right=']'),
        ' ', progressbar.ETA(), ' ', progressbar.FileTransferSpeed()]
  pbar = progressbar.ProgressBar(widgets=widgets, maxval=total_length).start()
  for chunk in response.iter_content(chunk_size=1):
    if chunk:
      f.write(chunk)
      f.flush()
    pbar.update(len(chunk) + 1)
  pbar.finish()

运行结果:

Python HTTP下载文件并显示下载进度条功能的实现

到此这篇关于Python HTTP下载文件并显示下载进度条功能的实现的文章就介绍到这了,更多相关python下载文件显示进度条内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python私有属性和方法实例分析
Jan 15 Python
详解Python编程中time模块的使用
Nov 20 Python
使用Python来开发Markdown脚本扩展的实例分享
Mar 04 Python
Python用csv写入文件_消除空余行的方法
Jul 06 Python
python如何求解两数的最大公约数
Sep 27 Python
Python 模拟动态产生字母验证码图片功能
Dec 24 Python
python实现opencv+scoket网络实时图传
Mar 20 Python
Python中的xlrd模块使用原理解析
May 21 Python
如何打包Python Web项目实现免安装一键启动的方法
May 21 Python
pycharm 关掉syntax检查操作
Jun 09 Python
详解如何修改python中字典的键和值
Sep 29 Python
用Python爬取英雄联盟的皮肤详细示例
Dec 06 Python
python实现将range()函数生成的数字存储在一个列表中
Apr 02 #Python
Python 给下载文件显示进度条和下载时间的实现
Apr 02 #Python
python求前n个阶乘的和实例
Apr 02 #Python
python实现将字符串中的数字提取出来然后求和
Apr 02 #Python
python对指定字符串逆序的6种方法(小结)
Apr 02 #Python
在python中实现求输出1-3+5-7+9-......101的和
Apr 02 #Python
django中url映射规则和服务端响应顺序的实现
Apr 02 #Python
You might like
PHP DataGrid 实现代码
2009/08/12 PHP
php 数组的一个悲剧?
2011/05/11 PHP
PHP检测用户是否关闭浏览器的方法
2016/02/14 PHP
如何修改yii2.0自带的user表为其它的表
2017/08/01 PHP
[原创]IE view-source 无法查看看源码 JavaScript看网页源码
2009/07/19 Javascript
javascript中如何处理引号编码"
2013/08/15 Javascript
jquery鼠标停止移动事件
2013/12/21 Javascript
JS定义网页表单提交(submit)的方法
2015/03/20 Javascript
js实现获取当前时间是本月第几周的方法
2015/08/11 Javascript
js中利用cookie实现记住密码功能
2020/08/20 Javascript
AngularJS服务service用法总结
2016/12/13 Javascript
Node.js中使用mongoose操作mongodb数据库的方法
2017/09/12 Javascript
一文让你彻底搞清楚javascript中的require、import与export
2017/09/24 Javascript
详解微信小程序的不同函数调用的几种方法
2019/05/08 Javascript
js+HTML5 canvas 实现简单的加载条(进度条)功能示例
2019/07/16 Javascript
微信小程序 多行文本显示...+显示更多按钮和收起更多按钮功能
2019/09/26 Javascript
[36:45]TNC vs VGJ.S 2018国际邀请赛小组赛BO2 第二场 8.18
2018/08/19 DOTA
深入理解Javascript中的this关键字
2015/03/27 Python
python通过文件头判断文件类型
2015/10/30 Python
深入解读Python解析XML的几种方式
2016/02/16 Python
浅谈python中的getattr函数 hasattr函数
2016/06/14 Python
Python实现针对给定单链表删除指定节点的方法
2018/04/12 Python
python中 * 的用法详解
2019/07/10 Python
django多对多表的创建,级联删除及手动创建第三张表
2019/07/25 Python
Python3 Click模块的使用方法详解
2020/02/12 Python
Python yield的用法实例分析
2020/03/06 Python
基于python tkinter的点名小程序功能的实例代码
2020/08/22 Python
Django-simple-captcha验证码包使用方法详解
2020/11/28 Python
夜大毕业自我鉴定
2013/10/11 职场文书
房地产销售经理岗位职责
2014/01/01 职场文书
家长给幼儿园的表扬信
2014/01/09 职场文书
英语感恩演讲稿
2014/01/14 职场文书
党委班子对照检查材料
2014/08/19 职场文书
小学生毕业评语
2014/12/26 职场文书
一文了解JavaScript用Element Traversal新属性遍历子元素
2021/11/27 Javascript
Vue操作Storage本地化存储
2022/04/29 Vue.js