本文整理汇总了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)