对python中的six.moves模块的下载函数urlretrieve详解
作者:仙界天堂 时间:2023-10-20 00:23:45
实验环境: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)#全部解压
抓取斗鱼首页
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文件的大小,以字节为单位
来源:https://blog.csdn.net/sinat_21585785/article/details/74177858
标签:python,six.moves,urlretrieve
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python 实现OpenCV格式和PIL.Image格式互转
2021-08-03 03:41:42
一文详解Golang中new和make的区别
2024-05-22 17:43:40
微信小程序实现签字功能
2024-04-16 09:26:12
![](https://img.aspxhome.com/file/2023/1/136681_0s.gif)
解读调用jupyter notebook文件内的函数一种简单方法
2021-04-19 09:47:56
![](https://img.aspxhome.com/file/2023/7/102757_0s.png)
Golang实现http重定向https
2024-04-26 17:27:57
php从文件夹随机读取文件的方法
2023-11-21 16:38:35
python集合的创建、添加及删除操作示例
2022-07-09 13:29:38
质量更好的tags标签效果
2008-06-04 12:24:00
![](https://img.aspxhome.com/file/UploadPic/20086/4/css-tags_35s.gif)
谈谈从phpinfo中能获取哪些值得注意的信息
2023-11-23 23:52:17
![](https://img.aspxhome.com/file/2023/0/111040_0s.png)
python不同系统中打开方法
2021-04-03 20:40:48
![](https://img.aspxhome.com/file/2023/7/97097_0s.png)
Python 包含汉字的文件读写之每行末尾加上特定字符
2022-02-01 13:13:46
MySQL 存储过程中执行动态SQL语句的方法
2024-01-12 21:22:22
SQL 2008的变更数据捕获——跟踪可变部分
2009-03-20 11:47:00
![](https://img.aspxhome.com/file/UploadPic/20093/2009322183639136.jpg)
Vue3+TS+Vite+NaiveUI搭建一个项目骨架实现
2024-05-28 15:55:14
![](https://img.aspxhome.com/file/2023/0/123120_0s.gif)
pycharm 使用tab跳出正在编辑的括号(){}{}等问题
2023-06-14 03:17:55
![](https://img.aspxhome.com/file/2023/0/60300_0s.jpg)
Python操作JSON实现网络数据交换
2023-01-27 02:21:53
PHP概率计算函数汇总
2023-11-19 08:06:19
Python排序搜索基本算法之堆排序实例详解
2021-07-10 12:35:30
![](https://img.aspxhome.com/file/2023/9/103609_0s.png)
关于导航的探讨
2008-03-21 12:04:00
Python PyWebIO实现网页版数据查询器
2023-07-11 20:01:03
![](https://img.aspxhome.com/file/2023/4/83724_0s.jpg)