本文整理匯總了Python中urllib.request.FancyURLopener方法的典型用法代碼示例。如果您正苦於以下問題:Python request.FancyURLopener方法的具體用法?Python request.FancyURLopener怎麽用?Python request.FancyURLopener使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類urllib.request
的用法示例。
在下文中一共展示了request.FancyURLopener方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _download_and_extract
# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import FancyURLopener [as 別名]
def _download_and_extract(self, url, extract_to, ext='zip'):
def _progress(count, block_size, total_size):
if total_size > 0:
print('\r>> Downloading %s %.1f%%' % (url,
float(count * block_size) / float(total_size) * 100.0), end=' ')
else:
print('\r>> Downloading %s' % (url), end=' ')
sys.stdout.flush()
urlretrieve = FancyURLopener().retrieve
local_zip_path = os.path.join(self.data_dir, 'tmp.' + ext)
urlretrieve(url, local_zip_path, _progress)
sys.stdout.write("\n>> Finished downloading. Unzipping...\n")
if ext == 'zip':
with zipfile.ZipFile(local_zip_path, "r") as zip_ref:
zip_ref.extractall(extract_to)
else:
with rarfile.RarFile(local_zip_path, "r") as zip_ref:
zip_ref.extractall(extract_to)
sys.stdout.write(">> Finished unzipping.\n")
os.remove(local_zip_path)
self.clear_statistics()
示例2: download
# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import FancyURLopener [as 別名]
def download(url, destination, tmp_dir='/tmp'):
def _progress(count, block_size, total_size):
sys.stdout.write('\rDownloading %s %.1f%%' % (url,
float(count * block_size) / float(total_size) * 100.0))
sys.stdout.flush()
urlretrieve = FancyURLopener().retrieve
if url.endswith('.zip'):
local_zip_path = os.path.join(tmp_dir, 'datasets_download.zip')
urlretrieve(url, local_zip_path, _progress)
with zipfile.ZipFile(local_zip_path, "r") as zip_ref:
zip_ref.extractall(extract_to)
os.remove(local_zip_path)
else:
urlretrieve(url, destination, _progress)