機械学習_pythonによるインターネットからのデータの自動ダウンロード

1172 ワード

                     ,                  ,            ,        ,              ,               ,           ,                      python         。


      :os、tarfile、urllib

import os
import tarfile
import urllib
from urllib.request import urlretrieve
def download(url,savepath):
    """
    :param url:     
    :param savepath:    
    :return:
    """
    filename=os.path.basename(url)
    filepath=os.path.join(savefile,filename)
    if not os.path.isdir(savefile):
        #               ,       
        os.makedirs(savefile)
        
        #    
        urlretrieve(url,filepath)
        
        #       
        downfile=tarfile.open(filepath)
        
        #       
        downfile.extractall(savefile)
        
        #      
        downfile.close()
        print("download finished")
    else:
        print("file has existed")
if __name__ == '__main__':
    url="https://raw.githubusercontent.com/ageron/handson-ml/master/datasets/housing/housing.tgz"
    savefile="./chapter1/data"

 
もちろんreporthook関数を定義して、ダウンロードの進捗状況を簡単に表示することができて、ここでは使えないので、書いていません