2016-11-11 81 views
1

我可以找到一些doc解釋如何使用tqdm包,但我不知道如何在線下載數據時如何生成進度表。如何在python中使用`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包顯示下載進度?

感謝

回答

1

截至目前,我做這樣的事情:

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

感謝精靈寶鑽,但下面的工作,使我更有意義。

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) 
+0

所以基本上你做兩個http請求下載單個文件。 效率不高,如果目標網址經歷了動態處理,則效率更高。 – silmaril

相關問題