【问题标题】:I can't make the download progress bar from tqdm to show the progress我无法从 tqdm 制作下载进度条来显示进度
【发布时间】:2019-05-28 06:29:48
【问题描述】:

我想用 tqdm 添加一个下载进度条。问题是它向我展示了这个:

0%|                                                                           | 0/11535.92578125 [00:00<?, ?KB/s]

它会下载文件而不显示任何进度。这是我的代码:

s = requests.Session()
r = s.post(url, login_data)
response = s.get(link_to_pdf, stream=True)
total_size = int(response.headers['content-length'])
# download the pdf
print(pdf_filename)
with open(pdf_filename + '.pdf', 'wb') as f:
    for data in tqdm(iterable=response.iter_content(chunk_size=chunk_size), total=total_size/chunk_size, unit='KB'):
        f.write(response.content)

【问题讨论】:

    标签: python python-3.x tqdm


    【解决方案1】:

    您没有将您写入请求的数据写入 1 次而不是分块,只需将您写入文件的 response.content 替换为您从 tqdm 获得的数据裂缝中的文件

      with open(pdf_filename + '.pdf', 'wb') as f:
        for data in tqdm(iterable=response.iter_content(chunk_size=chunk_size), total=total_size / chunk_size, unit='KB'):
            f.write(data)
    

    【讨论】:

    • @Iakovos Belonias 这有帮助吗?所以请投票
    • 是的,非常感谢。我这边犯了一个愚蠢的错误。
    猜你喜欢
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 2022-12-18
    • 2021-10-26
    • 2014-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多