from six.moves import urllib

实验环境: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)#全部解压
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

这里写图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值