python中的six.moves模块的下载函数urlretrieve
来源:互联网 发布:软件作业互助组 编辑:程序博客网 时间:2024/05/25 19:55
实验环境: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 urllibimport osimport sysimport tensorflow as tfimport tarfileFLAGS = 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')#指定解压路径和解压方式为解压gziptar.extractall(directory)#全部解压
抓取斗鱼首页
from six.moves import urllibimport osimport sysimport tensorflow as tfFLAGS = 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.htmlfilepath = 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文件的大小,以字节为单位
阅读全文
0 0
- python中的six.moves模块的下载函数urlretrieve
- 关于python 中的 urlretrieve() 函数
- python 下载 保存 图片的urllib.urlretrieve()函数 简单用法
- Python 的 six模块简介
- python内置map和six.moves.map的区别
- Python urllib的urlretrieve()函数解析
- Python urllib的urlretrieve()函数解析
- Python urllib的urlretrieve()函数解析
- python之urllib模块urlretrieve()函数初窥
- 使用python中的urlretrieve下批量下载数据
- urlretrieve下载的问题
- Python urllib模块urlretrieve()详解
- Python urllib的urlretrieve()函数解析Python的爬虫函数
- urlretrieve函数【远程数据下载到本地的函数】
- [python]解决urllib.urlretrieve()下载不完全的问题
- Python urllib模块urlopen()【打开网站,写入本地文件】与urlretrieve()【直接下载网站文件到本地】
- Python urllib模块urlopen()与urlretrieve()详解
- Python urllib模块urlopen()与urlretrieve()详解
- JDBC驱动类加载
- php面试题目分享(1)
- 复制转发CSDN博客快捷方法
- 【剑指Offer】面试题28:字符串的排序
- Spring-IOC原理及装配Bean(XML方式)
- python中的six.moves模块的下载函数urlretrieve
- PAT甲级 1129. Recommendation System (25)
- oracle drop、truncate 分区表分区注意
- 1.4 Getting Started
- android纯代码方式适配屏幕
- 程序员面试金典:字符串--确定字符串互异、原串翻转、空格替换
- XShell + Xmanager Forward X11 through tunnelling
- Target Sum
- Leetcode 89 Gray Code