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

yipeiwu_com5年前Python基础

实验环境: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之坑爹的字符编码

跟老齐学Python之坑爹的字符编码

字符编码,在编程中,是一个让学习者比较郁闷的东西,比如一个str,如果都是英文,好说多了。但恰恰不是如此,中文是我们不得不用的。所以,哪怕是初学者,都要了解并能够解决字符编码问题。...

Python信息抽取之乱码解决办法

Python信息抽取之乱码解决办法 就事论事,直说自己遇到的情况,和我不一样的路过吧,一样的就看看吧   信息抓取,用python,beautifulSoup,lxml,re,urlli...

Python闭包之返回函数的函数用法示例

Python闭包之返回函数的函数用法示例

闭包(closure)不是什么可怕的东西。如果用对了地方,它们其实可以很强大。闭包就是由其他函数动态生成并返回的函数,通俗地讲,在一个函数的内部,还有一个“内层”的函数,这个“内层”的函...

python实现自动登录后台管理系统

python实现自动登录后台管理系统

本文实例为大家分享了python实现自动登录后台管理系统的具体代码,供大家参考,具体内容如下 首先感谢下网络上的各位大神和博主,通过学习各位大神的文章,才实现了该脚本 ①首先浏览器运行真...

Python cookbook(数据结构与算法)实现查找两个字典相同点的方法

Python cookbook(数据结构与算法)实现查找两个字典相同点的方法

本文实例讲述了Python实现查找两个字典相同点的方法。分享给大家供大家参考,具体如下: 问题:寻找两个字典中间相同的地方(相同的键、相同的值等) 解决方案:通过keys()或者item...