对python中的six.moves模块的下载函数urlretrieve详解


Posted in Python onDecember 19, 2018

实验环境:windows 7,anaconda 3(python 3.5),tensorflow(gpu/cpu)

函数介绍:所用函数为six.moves下的urllib中的函数,调用如下urllib.request.urlretrieve(url,[filepath,[recall_func,[data]]])。简单介绍一下,url是必填的指的是下载地址,filepath指的是保存的本地地址,recall_func指的是回调函数,下载过程中会调用可以用来显示下载进度。

实验代码:以下载cifar10的dataset和抓取斗鱼首页为例

下载cifar10的dataset,并解压

from six.moves import urllib
import os
import sys
import tensorflow as tf
import tarfile
FLAGS = tf.app.flags.FLAGS#提取系统参数作用的变量
tf.app.flags.DEFINE_string('dir','D:/download_html','directory of html')#将下载目录保存到变量dir中,通过FLAGS.dir提取
directory = FLAGS.dir#从FLAGS中提取dir变量
url = 'http://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz'
filename = url.split('/')[-1]#-1表示分割后的最后一个元素
filepath = os.path.join(directory,filename)
if not os.path.exists(directory):
 os.makedirs(directory)
if not os.path.exists(filepath):
 def _recall_func(num,block_size,total_size):
 sys.stdout.write('\r>> downloading %s %.1f%%' % (filename,float(num*block_size)/float(total_size)*100.0))
 sys.stdout.flush()
 urllib.request.urlretrieve(url,filepath,_recall_func)
 print()
 file_info = os.stat(filepath)
 print('Successfully download',filename,file_info.st_size,'bytes')
tar = tarfile.open(filepath,'r:gz')#指定解压路径和解压方式为解压gzip
tar.extractall(directory)#全部解压

对python中的six.moves模块的下载函数urlretrieve详解

抓取斗鱼首页

from six.moves import urllib
import os
import sys
import tensorflow as tf
FLAGS = tf.app.flags.FLAGS#提取系统参数作用的变量
tf.app.flags.DEFINE_string('dir','D:/download_html','directory of html')#将下载目录保存到变量dir中,通过FLAGS.dir提取
directory = FLAGS.dir#从FLAGS中提取dir变量
url = 'http://www.douyu.com/'
filename = 'douyu.html'#保存成你想要的名字,这里保存成douyu.html
filepath = os.path.join(directory,filename)
if not os.path.exists(directory):
 os.makedirs(directory)
if not os.path.exists(filepath):
 def _recall_func(num,block_size,total_size):
 sys.stdout.write('\r>> downloading %s %.1f%%' % (filename,float(num*block_size)/float(total_size)*100.0))
 sys.stdout.flush()
 urllib.request.urlretrieve(url,filepath,_recall_func)
 print()
 file_info = os.stat(filepath)#获取文件信息
 print('Successfully download',filename,file_info.st_size,'bytes')#.st_size文件的大小,以字节为单位

对python中的six.moves模块的下载函数urlretrieve详解

对python中的six.moves模块的下载函数urlretrieve详解

以上这篇对python中的six.moves模块的下载函数urlretrieve详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
介绍Python的Django框架中的静态资源管理器django-pipeline
Apr 25 Python
在Python中操作时间之tzset()方法的使用教程
May 22 Python
Python实现字符串格式化的方法小结
Feb 20 Python
Python实现的插入排序算法原理与用法实例分析
Nov 22 Python
python3 面向对象__类的内置属性与方法的实例代码
Nov 09 Python
解决django后台样式丢失,css资源加载失败的问题
Jun 11 Python
详解pandas DataFrame的查询方法(loc,iloc,at,iat,ix的用法和区别)
Aug 02 Python
关于numpy中eye和identity的区别详解
Nov 29 Python
Jupyter Notebook远程登录及密码设置操作
Apr 10 Python
django创建超级用户时指定添加其它字段方式
May 14 Python
拿来就用!Python批量合并PDF的示例代码
Aug 10 Python
宝塔更新Python及Flask项目的部署
Apr 11 Python
python爬虫URL重试机制的实现方法(python2.7以及python3.5)
Dec 18 #Python
对python3标准库httpclient的使用详解
Dec 18 #Python
python 3.3 下载固定链接文件并保存的方法
Dec 18 #Python
python根据url地址下载小文件的实例
Dec 18 #Python
如何用python写一个简单的词法分析器
Dec 18 #Python
详解Python requests 超时和重试的方法
Dec 18 #Python
解决新django中的path不能使用正则表达式的问题
Dec 18 #Python
You might like
一篇不错的PHP基础学习笔记
2007/03/18 PHP
PHP XML数据解析代码
2010/05/26 PHP
PHP中删除变量时unset()和null的区别分析
2011/01/27 PHP
php取得字符串首字母的方法
2015/03/25 PHP
Codeigniter的dom类用法实例
2015/06/26 PHP
详谈PHP编码转换问题
2015/07/28 PHP
php网页版聊天软件实现代码
2016/08/12 PHP
浅谈laravel框架与thinkPHP框架的区别
2019/10/23 PHP
jquery.ui.draggable中文文档(原文翻译)
2013/11/15 Javascript
js制作简易年历完整实例
2015/01/28 Javascript
JavaScript实现文本框中默认显示背景图片在获得焦点后消失的方法
2015/07/01 Javascript
使用RequireJS库加载JavaScript模块的实例教程
2016/06/06 Javascript
js实现图片360度旋转
2017/01/22 Javascript
JS中SetTimeout和SetInterval使用初探
2017/03/23 Javascript
jQuery实现鼠标经过显示动画边框特效
2017/03/24 jQuery
在vue中实现简单页面逆传值的方法
2017/11/27 Javascript
微信小程序实现打卡日历功能
2020/09/21 Javascript
使用Vue 实现滑动验证码功能
2019/06/27 Javascript
LayUI动态设置checkbox不显示的解决方法
2019/09/02 Javascript
Python3里的super()和__class__使用介绍
2015/04/23 Python
整理Python最基本的操作字典的方法
2015/04/24 Python
python开发中module模块用法实例分析
2015/11/12 Python
Linux CentOS Python开发环境搭建教程
2018/11/28 Python
对python条件表达式的四种实现方法小结
2019/01/30 Python
python实现经纬度采样的示例代码
2020/12/10 Python
利用CSS3制作简单的3d半透明立方体图片展示
2017/03/25 HTML / CSS
德国高尔夫商店:Golfshop.de
2019/06/22 全球购物
武汉东之林科技有限公司机试
2013/09/17 面试题
电信专业应届生自荐信
2013/09/28 职场文书
奥巴马演讲稿
2014/01/08 职场文书
开学季活动策划方案
2014/02/28 职场文书
给小学生的新年寄语
2014/04/04 职场文书
幼儿园小班评语大全
2014/04/17 职场文书
赵氏孤儿观后感
2015/06/09 职场文书
小组组名及励志口号
2015/12/24 职场文书
五年级作文之成长
2019/09/16 职场文书