我想用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)
答案 0 :(得分:1)
您没有一次性写入请求中编写的数据,而不是分块地写,jsut替换了您从tqdm获得的数据片段中写入文件的response.content
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)