對(duì)python中的six.moves模塊的下載函數(shù)urlretrieve詳解
實(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 urllib import os import sys import tensorflow as tf import tarfile FLAGS = 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')#指定解壓路徑和解壓方式為解壓gzip tar.extractall(directory)#全部解壓
抓取斗魚(yú)首頁(yè)
from six.moves import urllib import os import sys import tensorflow as tf FLAGS = 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.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文件的大小,以字節(jié)為單位
以上這篇對(duì)python中的six.moves模塊的下載函數(shù)urlretrieve詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- python3.6使用urllib完成下載的實(shí)例
- python根據(jù)url地址下載小文件的實(shí)例
- Python爬取qq music中的音樂(lè)url及批量下載
- 【Python】Python的urllib模塊、urllib2模塊批量進(jìn)行網(wǎng)頁(yè)下載文件
- Python使用urllib2模塊實(shí)現(xiàn)斷點(diǎn)續(xù)傳下載的方法
- python基于urllib實(shí)現(xiàn)按照百度音樂(lè)分類下載mp3的方法
- python通過(guò)urllib2爬網(wǎng)頁(yè)上種子下載示例
- python使用urllib模塊開(kāi)發(fā)的多線程豆瓣小站mp3下載器
- python3獲取文件中url內(nèi)容并下載代碼實(shí)例
相關(guān)文章
如何用python開(kāi)發(fā)Zeroc Ice應(yīng)用
這篇文章主要介紹了如何用python開(kāi)發(fā)Zeroc Ice應(yīng)用,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-01-01淺析Python中壓縮zipfile與解壓縮tarfile模塊的使用
Python?提供了兩個(gè)標(biāo)準(zhǔn)庫(kù)模塊來(lái)處理文件的壓縮和解壓縮操作:zipfile和tarfile,本文將分享?這兩個(gè)模塊的使用方法,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10python爬蟲(chóng) 正則表達(dá)式使用技巧及爬取個(gè)人博客的實(shí)例講解
下面小編就為大家?guī)?lái)一篇python爬蟲(chóng) 正則表達(dá)式使用技巧及爬取個(gè)人博客的實(shí)例講解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10基于Python編寫(xiě)簡(jiǎn)易文字語(yǔ)音轉(zhuǎn)換器
這篇文章主要為大家介紹了如何利用Python編寫(xiě)一個(gè)簡(jiǎn)易文字語(yǔ)音轉(zhuǎn)換器,并打包成exe。文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編一起嘗試一下2022-03-03Python 等分切分?jǐn)?shù)據(jù)及規(guī)則命名的實(shí)例代碼
這篇文章主要介紹了Python 等分切分?jǐn)?shù)據(jù)及規(guī)則命名的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08Python進(jìn)程間通信multiprocess代碼實(shí)例
這篇文章主要介紹了Python進(jìn)程間通信multiprocess代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03Python函數(shù)基礎(chǔ)實(shí)例詳解【函數(shù)嵌套,命名空間,函數(shù)對(duì)象,閉包函數(shù)等】
這篇文章主要介紹了Python函數(shù)基礎(chǔ),結(jié)合實(shí)例形式詳細(xì)分析了函數(shù)嵌套,命名空間,函數(shù)對(duì)象,閉包函數(shù)等相關(guān)概念、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下2019-03-03Anaconda+VSCode配置tensorflow開(kāi)發(fā)環(huán)境的教程詳解
Anaconda是一個(gè)開(kāi)源的python發(fā)行版本,是現(xiàn)在比較流行的python數(shù)據(jù)科學(xué)平臺(tái),可以對(duì)python的科學(xué)包做到有效管理。這篇文章主要介紹了Anaconda+VSCode配置tensorflow開(kāi)發(fā)環(huán)境,需要的朋友可以參考下2020-03-03