【Python】使用urllib下载网络数据集

 # -*- coding:utf-8 -*-

import os
import sys
import tarfile
import urllib


def download_from_url(url,dir):
    file_name = url.split('/')[-1]
    file_path = os.path.join(dir,file_name)
    
    def print_progress(count, block_size, total_size):
        sys.stdout.write('\r>> Downloading %s %.1f%%' % (file_name, float(count * block_size) / float(total_size) * 100.0))
        sys.stdout.flush()
    
    if not os.path.exists(dir):
        os.makedirs(dir)
        
    if not os.path.exists(file_path):
        print('Start downloading...')
        urllib.request.urlretrieve(url,file_path,print_progress)
    else:
        print('File already exists.')
    return file_path

def extract_file(file_path,dest_path):
    if os.path.exists(file_path):
        file = tarfile.open(file_path)
        file.extractall(dest_path)
        file.close()
        print('Extraction has been completed.')
        
if __name__ == '__main__':
    FILE_URL = 'http://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz'
    FILE_DIR = os.path.join('datasets','CIFAR10')
    
    loaded_file_path = download_from_url(FILE_URL,FILE_DIR)
    extract_file(loaded_file_path,FILE_DIR)

Reference

[1] https://www.cnblogs.com/leokale-zz/p/11191906.html

上一篇:Python爬取有道词典,有道的反爬很难吗?也就这样啊!


下一篇:python实现发送信息到微信(调用捷易快信接口)