资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

urlretrieve下载函数怎么在python中使用-创新互联

这篇文章主要介绍了urlretrieve下载函数怎么在python中使用,创新互联成都网站设计公司小编觉得不错,现在分享给大家,也给大家做个参考,一起跟随创新互联成都网站设计公司小编来看看吧!

创新互联公司是专业的会宁网站建设公司,会宁接单;提供做网站、网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行会宁网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

Python主要用来做什么

Python主要应用于:1、Web开发;2、数据科学研究;3、网络爬虫;4、嵌入式应用开发;5、游戏开发;6、桌面应用开发。

实验环境: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)#全部解压

urlretrieve下载函数怎么在python中使用

抓取斗鱼首页

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文件的大小,以字节为单位

以上就是创新互联成都网站设计公司小编为大家收集整理的urlretrieve下载函数怎么在python中使用,如何觉得创新互联成都网站设计公司网站的内容还不错,欢迎将创新互联成都网站设计公司网站推荐给身边好友。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、网站设计器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站栏目:urlretrieve下载函数怎么在python中使用-创新互联
本文URL:http://cdkjz.cn/article/cdpcgc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220