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

一、前言
练习爬虫的小伙伴,在爬取数据比较多的时候,有时候等候的时间比较久一点,因为不知道具体的进度,可能会感到一丝丝无聊
本篇文章的主角“tqdm”可以很好地解决这个问题,让你的工程进度显然易见。
二、tqdm
- 官方文档:
https://pypi.org/project/tqdm/
- 安装:
pip install tqdm
三、简单的应用
- 参数说明
参数 | 说明 |
---|---|
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}]
参数 | 说明 |
---|---|
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)
实列二:
# -*- 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:
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
本文仅用于交流学习,未经作者允许,禁止转载,更勿做其他用途,违者必究。

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)