python 實現檔案檔案

來源:互聯網
上載者:User

標籤:while   下載   res   .net   code   實現   檔案   str   under   

Requests庫,高度封裝的http庫

import requestsurl = ‘http://down.sandai.net/thunder9/Thunder9.0.18.448.exe‘filename = url.split(‘/‘)[-1]  #擷取檔案名稱r = requests.get(url,stream = True)with open(filename,‘wb‘) as f:    p = 0  #下載計數器    chunk_size = 4096 #塊大小    try:        while True:            for data in r.iter_content(chunk_size):                p += f.write(data)                                print(‘%d‘ % (p))            except Exception as e:        print(e)    finally:        print(‘下載完畢!‘)        

urllib庫,操作上能比Requests靈活一點,沒有特殊需求的話基本沒什麼差別

import urllib.requesturl = ‘http://down.sandai.net/thunder9/Thunder9.0.18.448.exe‘resp = urllib.request.urlopen(url)filename = url.split(‘/‘)[-1]with open(filename,‘wb‘) as f:    p = 0 #下載計數    buffsize = 4096 #塊大小    try:        while True:            buff = resp.read(buffsize)            if not buff:    #buff為空白,即下載完畢,結束迴圈                break            p += f.write(buff)            print(‘%d‘ % p)    except Exception as e:        print(e)    finally:        print(‘下載完畢!‘)   

還有個一句話的 

import urlliburllib.urlretrieve(url, ‘test.jpg’)

 

python 實現檔案檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.