对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 相关文章推荐
使用cx_freeze把python打包exe示例
Jan 24 Python
Python映射拆分操作符用法实例
May 19 Python
使用pyecharts在jupyter notebook上绘图
Apr 23 Python
浅谈python正则的常用方法 覆盖范围70%以上
Mar 14 Python
在Mac上删除自己安装的Python方法
Oct 29 Python
Python操作配置文件ini的三种方法讲解
Feb 22 Python
简单了解Python3里的一些新特性
Jul 13 Python
Python TCP通信客户端服务端代码实例
Nov 21 Python
Python 读取 YUV(NV12) 视频文件实例
Dec 09 Python
python实现word文档批量转成自定义格式的excel文档的思路及实例代码
Feb 21 Python
Pytorch框架实现mnist手写库识别(与tensorflow对比)
Jul 20 Python
Django+Uwsgi+Nginx如何实现生产环境部署
Jul 31 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编写简单的App接口
2016/08/28 PHP
php实现通过soap调用.Net的WebService asmx文件
2017/02/27 PHP
php usort 使用用户自定义的比较函数对二维数组中的值进行排序
2017/05/02 PHP
PHP 实现 JSON 数据的编码和解码操作详解
2020/04/22 PHP
jquery数组过滤筛选方法grep()简介
2014/06/06 Javascript
jquery实现简单文字提示效果
2015/12/02 Javascript
使用Sticky组件实现带sticky效果的tab导航和滚动导航的方法
2016/03/22 Javascript
浅谈jQuery添加的HTML,JS失效的问题
2016/10/05 Javascript
vue.js中mint-ui框架的使用方法
2017/05/12 Javascript
ng-events类似ionic中Events的angular全局事件
2018/09/05 Javascript
Vue在页面数据渲染完成之后的调用方法
2018/09/11 Javascript
微信小程序实现通过双向滑动缩放图片大小的方法
2018/12/30 Javascript
vue 判断元素内容是否超过宽度的方式
2020/07/29 Javascript
js实现点击选项置顶动画效果
2020/08/25 Javascript
JS绘图Flot如何实现可选显示曲线图功能
2020/10/16 Javascript
解决Vue-cli无法编译es6的问题
2020/10/30 Javascript
[46:12]完美世界DOTA2联赛循环赛 DM vs Matador BO2第一场 11.04
2020/11/04 DOTA
python通过定义一个类实例作为ftp回调方法
2015/05/04 Python
Python备份目录及目录下的全部内容的实现方法
2016/06/12 Python
Python实现爬取需要登录的网站完整示例
2017/08/19 Python
Python中pygal绘制雷达图代码分享
2017/12/07 Python
django2 快速安装指南分享
2018/01/05 Python
pandas实现选取特定索引的行
2018/04/20 Python
利用python如何处理百万条数据(适用java新手)
2018/06/06 Python
Python generator生成器和yield表达式详解
2019/08/08 Python
Python实现计算长方形面积(带参数函数demo)
2020/01/18 Python
html5图片上传预览示例分享
2014/04/14 HTML / CSS
全球速卖通西班牙站:AliExpress西班牙
2017/10/30 全球购物
GafasWorld西班牙:购买太阳镜、眼镜和隐形眼镜
2019/09/08 全球购物
C语言基础笔试题
2013/04/27 面试题
年度考核自我鉴定
2013/11/09 职场文书
幼儿园毕业典礼主持词
2014/03/21 职场文书
师范生见习报告范文
2014/11/03 职场文书
退休欢送会主持词
2015/07/01 职场文书
餐厅服务员管理制度
2015/08/05 职场文书
Nginx反爬虫策略,防止UA抓取网站
2021/03/31 Servers