2016-11-11 5 views
1

tqdmパッケージの使い方を説明しているdocがありますが、データをオンラインでダウンロードする際にプログレスメーターの作成方法を理解できません。以下は 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

したがって、基本的に1つのファイルをダウンロードするための2つのhttp要求を行います。 あまり効率的ではありません。ターゲットURLが動的処理を行う場合はさらに効率的です。 – silmaril

関連する問題