利用Python下載文件的方法有哪些?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
urllib.request
主要使用的是urlretrieve方法,該方法處理待淘汰的方法,不建議使用。
import urllib.request url = '/file/tupian/20230213/21696.png' urllib.request.urlretrieve(url, './image/logo.png')
requests
相比上述方案,可以返回HTTP的meta信息。
import requests r = requests.get(url) with open('./image/logo.png', 'wb') as f: f.write(r.content) # Retrieve HTTP meta-data print(r.status_code) print(r.headers['content-type']) print(r.encoding)
wget
wget是Linux下的一個命令行下載工具,在Python中可以直接通過安裝包后使用。使用方法如下:
import wget url = '/file/tupian/20230213/21696.png' wget.download(url, './image/logo.png')
看完上述內(nèi)容,你們掌握利用Python下載文件的方法有哪些的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!