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 相关文章推荐
10款最好的Web开发的 Python 框架
Mar 18 Python
itchat和matplotlib的结合使用爬取微信信息的实例
Aug 25 Python
Python3实现转换Image图片格式
Jun 21 Python
python中使用zip函数出现错误的原因
Sep 28 Python
如何通过python画loss曲线的方法
Jun 26 Python
python实现各种插值法(数值分析)
Jul 30 Python
Python3实现配置文件差异对比脚本
Nov 18 Python
python之pymysql模块简单应用示例代码
Dec 16 Python
Django 路由层URLconf的实现
Dec 30 Python
tensorflow 20:搭网络,导出模型,运行模型的实例
May 26 Python
matplotlib grid()设置网格线外观的实现
Feb 22 Python
Python实现提取PDF简历信息并存入Excel
Apr 02 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学习笔记 IIS7下安装配置php环境
2012/10/29 PHP
PHP类与对象中的private访问控制的疑问
2012/11/01 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(七)
2014/06/23 PHP
php使用GD实现颜色渐变实例
2015/06/02 PHP
YII框架中搜索分页jQuery写法详解
2016/12/19 PHP
PHP实现留言板功能的详细代码
2017/03/25 PHP
TP5(thinkPHP5框架)实现显示错误信息及行号功能的方法
2019/06/03 PHP
xml转json的js代码
2012/08/28 Javascript
Jquery实现图片左右自动滚动示例
2013/09/25 Javascript
php析构函数的具体用法小结
2014/03/11 Javascript
jQuery中:animated选择器用法实例
2014/12/29 Javascript
nodejs 整合kindEditor实现图片上传
2015/02/03 NodeJs
怎么通过onclick事件获取js函数返回值(代码少)
2015/07/28 Javascript
JQuery EasyUI的使用
2016/02/24 Javascript
NodeJs——入门必看攻略
2016/06/27 NodeJs
jQuery实现字体颜色渐变效果的方法
2017/03/29 jQuery
Dropify.js图片宽高自适应的方法
2017/11/27 Javascript
Python中使用socket发送HTTP请求数据接收不完整问题解决方法
2015/02/04 Python
详解Python各大聊天系统的屏蔽脏话功能原理
2016/12/01 Python
详解python中字典的循环遍历的两种方式
2017/02/07 Python
Python 专题二 条件语句和循环语句的基础知识
2017/03/19 Python
python脚本监控Tomcat服务器的方法
2018/07/06 Python
python提取包含关键字的整行数据方法
2018/12/11 Python
Python读取excel文件中带公式的值的实现
2020/04/17 Python
anaconda升级sklearn版本的实现方法
2021/02/22 Python
台湾全方位线上课程与职能学习平台:TibaMe
2019/12/04 全球购物
参观监狱心得体会
2014/01/02 职场文书
2014自荐信的写作技巧
2014/01/28 职场文书
幼儿园老师寄语
2014/04/03 职场文书
内勤主管岗位职责
2014/04/03 职场文书
中秋节国旗下演讲稿
2014/09/13 职场文书
校运会广播稿
2015/08/19 职场文书
《暗黑破坏神2:重制版》本周进行第一轮A测 目前可官网进行申请报名
2021/04/07 其他游戏
redis三种高可用方式部署的实现
2021/05/11 Redis
「约定的梦幻岛」作画发布诺曼生日新绘
2022/03/21 日漫
【海涛七七解说】DCG第二周:DK VS 天禄
2022/04/01 DOTA