我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用six.moves.urllib.request.urlretrieve()。
def urlretrieve(url, filename, reporthook=None, data=None): def chunk_read(response, chunk_size=8192, reporthook=None): total_size = response.info().get('Content-Length').strip() total_size = int(total_size) count = 0 while 1: chunk = response.read(chunk_size) count += 1 if not chunk: reporthook(count, total_size, total_size) break if reporthook: reporthook(count, chunk_size, total_size) yield chunk response = urlopen(url, data) with open(filename, 'wb') as fd: for chunk in chunk_read(response, reporthook=reporthook): fd.write(chunk)
def maybe_download(url, filename, prefix, num_bytes=None): """Takes an URL, a filename, and the expected bytes, download the contents and returns the filename num_bytes=None disables the file size check.""" local_filename = None if not os.path.exists(os.path.join(prefix, filename)): try: print("Downloading file {}...".format(url + filename)) with tqdm(unit='B', unit_scale=True, miniters=1, desc=filename) as t: local_filename, _ = urlretrieve(url + filename, os.path.join(prefix,filename), reporthook=reporthook(t)) except AttributeError as e: print("An error occurred when downloading the file! Please get the dataset using a browser.") raise e # We have a downloaded file # Check the stats and make sure they are ok file_stats = os.stat(os.path.join(prefix,filename)) if num_bytes is None or file_stats.st_size == num_bytes: print("File {} successfully loaded".format(filename)) else: raise Exception("Unexpected dataset size. Please get the dataset using a browser.") return local_filename
def download_exchange_symbols(exchange_name, environ=None): """ Downloads the exchange's symbols.json from the repository. Parameters ---------- exchange_name: str environ: Returns ------- str """ filename = get_exchange_symbols_filename(exchange_name) url = SYMBOLS_URL.format(exchange=exchange_name) response = request.urlretrieve(url=url, filename=filename) return response
def urlretrieve(url, filename, reporthook=None, data=None): ''' This function is adpated from: https://github.com/fchollet/keras Original work Copyright (c) 2014-2015 keras contributors ''' def chunk_read(response, chunk_size=8192, reporthook=None): total_size = response.info().get('Content-Length').strip() total_size = int(total_size) count = 0 while 1: chunk = response.read(chunk_size) if not chunk: break count += 1 if reporthook: reporthook(count, chunk_size, total_size) yield chunk response = urlopen(url, data) with open(filename, 'wb') as fd: for chunk in chunk_read(response, reporthook=reporthook): fd.write(chunk)
def maybe_download(filename, expected_bytes, force=False): """Download a file if not present, and make sure it's the right size.""" if force or not os.path.exists(filename): print('Attempting to download:', filename) filename, _ = urlretrieve(url + filename, filename, reporthook=download_progress_hook) print('\nDownload Complete!') statinfo = os.stat(filename) if statinfo.st_size == expected_bytes: print('Found and verified', filename) else: raise Exception( 'Failed to verify ' + filename + \ '. Can you get to it with a browser?') return filename
def urlretrieve(url, filename, reporthook=None, data=None): def chunk_read(response, chunk_size=8192, reporthook=None): total_size = response.info().get('Content-Length').strip() total_size = int(total_size) count = 0 while 1: chunk = response.read(chunk_size) if not chunk: break count += 1 if reporthook: reporthook(count, chunk_size, total_size) yield chunk response = urlopen(url, data) with open(filename, 'wb') as fd: for chunk in chunk_read(response, reporthook=reporthook): fd.write(chunk)
def download(number, save_dir='./'): """Download pre-trained word vector :param number: integer, default ``None`` :param save_dir: str, default './' :return: file path for downloaded file """ df = load_datasets() row = df.iloc[[number]] url = ''.join(row.URL) if not url: print('The word vector you specified was not found. Please specify correct name.') widgets = ['Test: ', Percentage(), ' ', Bar(marker=RotatingMarker()), ' ', ETA(), ' ', FileTransferSpeed()] pbar = ProgressBar(widgets=widgets) def dlProgress(count, blockSize, totalSize): if pbar.max_value is None: pbar.max_value = totalSize pbar.start() pbar.update(min(count * blockSize, totalSize)) file_name = url.split('/')[-1] if not os.path.exists(save_dir): os.makedirs(save_dir) save_path = os.path.join(save_dir, file_name) path, _ = urlretrieve(url, save_path, reporthook=dlProgress) pbar.finish() return path
def download_model(url, folder="."): """Download a model.""" dest = path.join(folder, path.basename(url)) urlreq.urlretrieve(url, dest) return dest
def try_download(corpus_name): store_path = corpus_path(corpus_name) if os.path.exists(store_path): return True, store_path try: url = download_url(corpus_name) filename, header = urlretrieve(url, store_path) return True, filename except: print("Can't download: " + corpus_name) return False, None
def download_mnist_data(): print('Downloading {:s}...'.format(train_images)) request.urlretrieve('{:s}/{:s}'.format(parent, train_images), train_images) print('Done') print('Downloading {:s}...'.format(train_labels)) request.urlretrieve('{:s}/{:s}'.format(parent, train_labels), train_labels) print('Done') print('Downloading {:s}...'.format(test_images)) request.urlretrieve('{:s}/{:s}'.format(parent, test_images), test_images) print('Done') print('Downloading {:s}...'.format(test_labels)) request.urlretrieve('{:s}/{:s}'.format(parent, test_labels), test_labels) print('Done') print('Converting training data...') data_train, target_train = load_mnist(train_images, train_labels, num_train) print('Done') print('Converting test data...') data_test, target_test = load_mnist(test_images, test_labels, num_test) mnist = {} mnist['data'] = np.append(data_train, data_test, axis=0) mnist['target'] = np.append(target_train, target_test, axis=0) print('Done') print('Save output...') with open('mnist.pkl', 'wb') as output: six.moves.cPickle.dump(mnist, output, -1) print('Done') print('Convert completed')
def maybe_download(filename, expected_bytes): """Download a file if not present, and make sure it's the right size.""" if not os.path.exists(filename): print('Downloading ', filename, " ...") filename, _ = urlretrieve(url + filename, filename) statinfo = os.stat(filename) if statinfo.st_size == expected_bytes: print('Found and verified', filename) else: raise Exception('Failed to verify' + filename + '. Can you get to it with a browser?') else: print('Found and verified', filename) return filename
def maybe_download(url, download_path, filename): if not os.path.exists(os.path.join(download_path, filename)): try: print("Downloading file {}...".format(url + filename)) with TqdmUpTo(unit='B', unit_scale=True, miniters=1, desc=filename) as t: local_filename, _ = urlretrieve(url, os.path.join(download_path, filename), reporthook=t.update_to) except AttributeError as e: print("An error occurred when downloading the file! Please get the dataset using a browser.") raise e
def download_mnist_data(): print('Downloading {:s}...'.format(train_images)) request.urlretrieve('{:s}/{:s}'.format(parent, train_images), train_images) print('Done') print('Downloading {:s}...'.format(train_labels)) request.urlretrieve('{:s}/{:s}'.format(parent, train_labels), train_labels) print('Done') print('Downloading {:s}...'.format(test_images)) request.urlretrieve('{:s}/{:s}'.format(parent, test_images), test_images) print('Done') print('Downloading {:s}...'.format(test_labels)) request.urlretrieve('{:s}/{:s}'.format(parent, test_labels), test_labels) print('Done') print('Converting training data...') data_train, target_train = load_mnist(train_images, train_labels, num_train) print('Done') print('Converting test data...') data_test, target_test = load_mnist(test_images, test_labels, num_test) mnist = {'data': np.append(data_train, data_test, axis=0), 'target': np.append(target_train, target_test, axis=0)} print('Done') print('Save output...') with open('mnist.pkl', 'wb') as output: six.moves.cPickle.dump(mnist, output, -1) print('Done') print('Convert completed')
def urlretrieve(url, filename, reporthook=None, data=None): """Replacement for `urlretrive` for Python 2. Under Python 2, `urlretrieve` relies on `FancyURLopener` from legacy `urllib` module, known to have issues with proxy management. # Arguments url: url to retrieve. filename: where to store the retrieved data locally. reporthook: a hook function that will be called once on establishment of the network connection and once after each block read thereafter. The hook will be passed three arguments; a count of blocks transferred so far, a block size in bytes, and the total size of the file. data: `data` argument passed to `urlopen`. """ def chunk_read(response, chunk_size=8192, reporthook=None): total_size = response.info().get('Content-Length').strip() total_size = int(total_size) count = 0 while 1: chunk = response.read(chunk_size) count += 1 if not chunk: reporthook(count, total_size, total_size) break if reporthook: reporthook(count, chunk_size, total_size) yield chunk response = urlopen(url, data) with open(filename, 'wb') as fd: for chunk in chunk_read(response, reporthook=reporthook): fd.write(chunk)
def _download_recipe(file_url, filename, contrib): file_path = path.join(contrib, filename) request.urlretrieve(file_url, file_path)
def maybe_download(url, data_dir, filename, expected_bytes, force=False): """ Download a file if not present, and make sure it's the right size. """ if force or not os.path.exists(os.path.join(data_dir, filename)): filename, _ = urlretrieve(url + filename, os.path.join(data_dir, filename)) statinfo = os.stat(os.path.join(data_dir, filename)) if statinfo.st_size == expected_bytes: print('Found and verified', filename) else: raise Exception( 'Failed to verify' + filename + '. Can you get to it with a browser?') return filename
def download(): url = 'https://www.cs.toronto.edu/~kriz' request.urlretrieve(url+'/'+fname, fname)
def download(): url = 'http://yann.lecun.com/exdb/mnist' request.urlretrieve(url+'/'+train_image, train_image) request.urlretrieve(url+'/'+train_label, train_label) request.urlretrieve(url+'/'+test_image, test_image) request.urlretrieve(url+'/'+test_label, test_label)
def maybe_download(filename, expected_bytes): """Download a file if not present, and make sure it's the right size.""" if not os.path.exists(filename): filename, _ = urlretrieve(url + filename, filename) statinfo = os.stat(filename) if statinfo.st_size == expected_bytes: print('Found and verified %s' % filename) else: print(statinfo.st_size) raise Exception( 'Failed to verify ' + filename + '. Can you get to it with a browser?') return filename
def maybe_download(filename, expected_bytes, force=False): """Download a file if not present, and make sure it's the right size.""" if force or not os.path.exists(filename): print('Attempting to download:', filename) filename, _ = urlretrieve(url + filename, filename, reporthook=download_progress_hook) print('\nDownload Complete!') statinfo = os.stat(filename) if statinfo.st_size == expected_bytes: print('Found and verified', filename) else: raise Exception( 'Failed to verify ' + filename + '. Can you get to it with a browser?') return filename
def maybe_download(filename, work_directory): """Download the data from Yann's website, unless it's already here.""" if not os.path.exists(work_directory): os.mkdir(work_directory) filepath = os.path.join(work_directory, filename) if not os.path.exists(filepath): filepath, _ = urlretrieve(SOURCE_URL + filename, filepath) statinfo = os.stat(filepath) print('Succesfully downloaded', filename, statinfo.st_size, 'bytes.') return filepath
def maybe_download(filename, expected_bytes, force=False): """Download a file if not present, and make sure it's the right size.""" if force or not os.path.exists(filename): filename, _ = urlretrieve(url + filename, filename) statinfo = os.stat(filename) if statinfo.st_size == expected_bytes: print('Found and verified', filename) else: raise Exception( 'Failed to verify' + filename + '. Can you get to it with a browser?') return filename
def __delitem__(self, key): del self._inv[super(bidict, self).__getitem__(key)] return dict.__delitem__(self, key) # Under Python 2, 'urlretrieve' relies on FancyURLopener from legacy # urllib module, known to have issues with proxy management
def get_file(fname, origin, outdir): ''' Parameters ---------- fname: output file name origin: url, link outdir: path to output dir ''' fpath = os.path.join(outdir, fname) # ====== remove empty folder ====== # if os.path.exists(fpath): if os.path.isdir(fpath) and len(os.listdir(fpath)) == 0: shutil.rmtree(fpath) # ====== download package ====== # if not os.path.exists(fpath): prog = Progbar(target=-1, name="Downloading: %s" % origin, print_report=True, print_summary=True) def dl_progress(count, block_size, total_size): if prog.target < 0: prog.target = total_size else: prog.add(count * block_size - prog.seen_so_far) error_msg = 'URL fetch failure on {}: {} -- {}' try: try: urlretrieve(origin, fpath, dl_progress) except URLError as e: raise Exception(error_msg.format(origin, e.errno, e.reason)) except HTTPError as e: raise Exception(error_msg.format(origin, e.code, e.msg)) except (Exception, KeyboardInterrupt) as e: if os.path.exists(fpath): os.remove(fpath) raise return fpath # =========================================================================== # Python utilities # ===========================================================================
def setUpClass(cls): base_url = 'https://github.com/yuyu2172/' \ 'share-weights/releases/download/0.0.3' cls.dataset = np.load(request.urlretrieve(os.path.join( base_url, 'voc_detection_dataset_2007_test_truncated_2017_06_06.npz'))[0]) cls.result = np.load(request.urlretrieve(os.path.join( base_url, 'voc_detection_result_2007_test_truncated_2017_06_06.npz'))[0])
def maybe_download(filename, expected_bytes): """Download a file if not present, and make sure it's the right size.""" if not os.path.exists(filename): filename, _ = urlretrieve(url + filename, filename) statinfo = os.stat(filename) if statinfo.st_size == expected_bytes: print('Found and verified %s' % filename) else: print(statinfo.st_size) raise Exception( 'Failed to verify ' + filename + '. Can you get to it with a browser?') return filename # filename = maybe_download('/users1/zyli/data/text8.zip', 31344016)
def download_mnist_data(): print("Downloading {} ...".format(train_images_filename)) request.urlretrieve("{}/{}".format(parent, train_images_filename), train_images_filename) print("Downloading {} ...".format(train_labels_filename)) request.urlretrieve("{}/{}".format(parent, train_labels_filename), train_labels_filename) print("Downloading {} ...".format(test_images_filename)) request.urlretrieve("{}/{}".format(parent, test_images_filename), test_images_filename) print("Downloading {} ...".format(test_labels_filename)) request.urlretrieve("{}/{}".format(parent, test_labels_filename), test_labels_filename) print("Done")
def download_file(url, filename, quiet=True, reporthook_kwargs=None): """Downloads a file with optional progress report.""" if '://' not in url: raise ValueError("fully qualified URL required: %s" % url) if url.partition('://')[0] not in ('https', 'http', 'ftp'): raise ValueError("unsupported URL schema: %s" % url) if url.startswith('ftp://'): retrieve = _urlretrieve else: retrieve = _urlretrieve_requests if quiet: return retrieve(url, filename) reporthook_kwargs = reporthook_kwargs or {} if filename: reporthook_kwargs.setdefault('desc', filename) reporthook_kwargs.setdefault('unit', 'b') reporthook_kwargs.setdefault('unit_scale', True) reporthook = _ReportHook(**reporthook_kwargs) retrieve = _urlretrieve if url.startswith('ftp://') else _urlretrieve_requests with contextlib.closing(reporthook): retrieve(url, filename, reporthook)
def maybe_download(filename, force=False): """Download a file if not present""" filename = os.path.join(root, filename) if force or not os.path.exists(filename): print('Attempting to download:', filename) filename, _ = urlretrieve(url + filename, filename) print('Download Complete!') return filename