当前位置:   article > 正文

Python爬虫有用的库:tqdm,生成进度条_tqbm.update

tqbm.update


在这里插入图片描述

一、前言

练习爬虫的小伙伴,在爬取数据比较多的时候,有时候等候的时间比较久一点,因为不知道具体的进度,可能会感到一丝丝无聊
本篇文章的主角“tqdm”可以很好地解决这个问题,让你的工程进度显然易见。
在这里插入图片描述

二、tqdm

  • 官方文档:

https://pypi.org/project/tqdm/

  • 安装:
pip install tqdm
  • 1

在这里插入图片描述

三、简单的应用

  • 参数说明
参数说明
iterable : iterable, optional一个可迭代对象,比如迭代器、生成器、列表
desc : str, optional作为进度条说明,在进度条左边
total : int, optional预取的迭代次数
leave : bool, optional循环结束后是否保留进度提示信息, 默认为True
ncols : int, optional进度条长度
mininterval : float, optional进度条最小的更新间隔(秒)
maxinterval : float, optional进度条最大的更新间隔(秒)
unit单位,默认it每秒迭代数
postfix : str, optional在进度条右边添加字典类型描述信息
position指定偏移,这个功能在多个进度条中有用
bar_format自定义进度条
  • 自定义进度条的一些参数
bar_format='{l_bar}{bar}{r_bar}'

l_bar: {desc}: {percentage:3.0f}%|
bar: 进度条
r_bar: |{n_fmt}/{total_fmt}[{elapsed}<{remaining},{rate_fmt}{postfix}]

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
参数说明
percentage百分比
n_fmt当前数
total_fmt总数
elapsed消耗的时间
remaining剩余时间
rate_fmt速率
postifx后缀字典描述
desc、postfix默认为空
  • 实例:

实例一:

# -*- coding: UTF-8 -*-
"""
# @Time: 2021/8/8 10:39
# @Author: 远方的星
# @CSDN: https://blog.csdn.net/qq_44921056
"""
from tqdm import tqdm
import time

test = tqdm(iterable=range(10),
            desc='测试:',
            total=None,
            leave=True,
            ncols=None,
            mininterval=0.1,
            maxinterval=10.0,
            unit='it',
            bar_format=None,
            position=None,
            postfix='远方的星')
for i in test:
    time.sleep(0.5)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

在这里插入图片描述
实列二:

# -*- coding: UTF-8 -*-
"""
# @Time: 2021/8/8 10:39
# @Author: 远方的星
# @CSDN: https://blog.csdn.net/qq_44921056
"""
from tqdm import tqdm
import time

bar_format = '{desc}{percentage:3.0f}%|{bar}|{n_fmt}/{total_fmt}[{elapsed}<{remaining}{postfix}]'
total_d = 10  # 设置总数
with tqdm(total=total_d, bar_format=bar_format) as _tqdm:
    _tqdm.set_description('测试')  # 设置desc的值
    for i in range(10):
        time.sleep(0.5)
        _tqdm.set_postfix(author='远方的星')  # 设置postfix的值,传入的是一个字典
        _tqdm.update(1)  # 更新一次进度条的间隔,单位:秒
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

在这里插入图片描述
在这里插入图片描述

四、参考文章

参考文章1:
https://blog.csdn.net/CSDN_OWL/article/details/114335467
参考文章2:
https://blog.csdn.net/qq_27825451/article/details/95486373
参考文章3:
https://blog.csdn.net/qq_41554005/article/details/117297861

作者:远方的星
CSDN:https://blog.csdn.net/qq_44921056
本文仅用于交流学习,未经作者允许,禁止转载,更勿做其他用途,违者必究。

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

闽ICP备14008679号