我可以找到一些doc解释如何使用tqdm
包,但是我无法从中了解如何在线下载数据时生成进度表。
以下是我从ResidentMario复制的用于下载数据的示例代码
def download_file(url, filename):
"""
Helper method handling downloading large files from `url` to `filename`. Returns a pointer to `filename`.
"""
r = requests.get(url, stream=True)
with open(filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
return filename
dat = download_file("https://data.cityofnewyork.us/api/views/h9gi-nx95/rows.csv?accessType=DOWNLOAD",
"NYPD Motor Vehicle Collisions.csv")
有谁能告诉我如何在这里使用tqdm包来显示下载进度?
由于
答案 0 :(得分:3)
截至目前,我做了类似的事情:
def download_file(url, filename):
"""
Helper method handling downloading large files from `url` to `filename`. Returns a pointer to `filename`.
"""
chunkSize = 1024
r = requests.get(url, stream=True)
with open(filename, 'wb') as f:
pbar = tqdm( unit="B", total=int( r.headers['Content-Length'] ) )
for chunk in r.iter_content(chunk_size=chunkSize):
if chunk: # filter out keep-alive new chunks
pbar.update (len(chunk))
f.write(chunk)
return filename
答案 1 :(得分:0)
感谢silmaril,但下面的工作对我来说更有意义。
def download_file(url, filename):
testread = requests.head(url_r) # A HEAD request only downloads the headers
filelength = int(testread.headers['Content-length'])
r = requests.get(url, stream=True) # actual download full file
with open(filename, 'wb') as f:
pbar = tqdm(total=int(filelength/1024))
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
pbar.update ()
f.write(chunk)