国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

對(duì)python中的six.moves模塊的下載函數(shù)urlretrieve詳解

2020-02-16 00:12:23
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

實(shí)驗(yàn)環(huán)境:windows 7,anaconda 3(python 3.5),tensorflow(gpu/cpu)

函數(shù)介紹:所用函數(shù)為six.moves下的urllib中的函數(shù),調(diào)用如下urllib.request.urlretrieve(url,[filepath,[recall_func,[data]]])。簡(jiǎn)單介紹一下,url是必填的指的是下載地址,filepath指的是保存的本地地址,recall_func指的是回調(diào)函數(shù),下載過(guò)程中會(huì)調(diào)用可以用來(lái)顯示下載進(jìn)度。

實(shí)驗(yàn)代碼:以下載cifar10的dataset和抓取斗魚(yú)首頁(yè)為例

下載cifar10的dataset,并解壓

from six.moves import urllibimport osimport sysimport tensorflow as tfimport tarfileFLAGS = tf.app.flags.FLAGS#提取系統(tǒng)參數(shù)作用的變量tf.app.flags.DEFINE_string('dir','D:/download_html','directory of html')#將下載目錄保存到變量dir中,通過(guò)FLAGS.dir提取directory = FLAGS.dir#從FLAGS中提取dir變量url = 'http://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz'filename = url.split('/')[-1]#-1表示分割后的最后一個(gè)元素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')#指定解壓路徑和解壓方式為解壓gziptar.extractall(directory)#全部解壓

python six.moves模塊的下載函數(shù)urlretrieve

抓取斗魚(yú)首頁(yè)

from six.moves import urllibimport osimport sysimport tensorflow as tfFLAGS = tf.app.flags.FLAGS#提取系統(tǒng)參數(shù)作用的變量tf.app.flags.DEFINE_string('dir','D:/download_html','directory of html')#將下載目錄保存到變量dir中,通過(guò)FLAGS.dir提取directory = FLAGS.dir#從FLAGS中提取dir變量url = 'http://www.douyu.com/'filename = 'douyu.html'#保存成你想要的名字,這里保存成douyu.htmlfilepath = 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文件的大小,以字節(jié)為單位

python six.moves模塊的下載函數(shù)urlretrieve

python six.moves模塊的下載函數(shù)urlretrieve

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 高密市| 大姚县| 肥西县| 丰宁| 邵东县| 赣榆县| 格尔木市| 茌平县| 普兰县| 维西| 时尚| 高阳县| 平远县| 鹤壁市| 秭归县| 伊川县| 湘潭市| 张家港市| 大英县| 遂平县| 安新县| 长泰县| 江孜县| 肥西县| 平邑县| 体育| 东兰县| 蛟河市| 广宁县| 聂荣县| 万盛区| 壤塘县| 云南省| 萝北县| 舟山市| 高台县| 巴南区| 涟水县| 夹江县| 台山市| 元朗区|