python下载大文件
生活随笔
收集整理的這篇文章主要介紹了
python下载大文件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1. wget
def download_big_file_with_wget(url, target_file_name):"""使用wget下載大文件Note: 需要系統(tǒng)安裝wget"""import osimport subprocessdownload_process = subprocess.Popen(["wget", "-c", "-O", target_file_name, "'{}'".format(url)])download_process.wait()if not os.path.exists(target_file_name):raise Exception("fail to download file from {}".format(url))?
2. python核心庫
def download_big_file(url, target_file_name):"""使用python核心庫下載大文件ref: https://stackoverflow.com/questions/1517616/stream-large-binary-files-with-urllib2-to-file"""import sysif sys.version_info > (2, 7):# Python 3from urllib.request import urlopenelse:# Python 2from urllib2 import urlopenresponse = urlopen(url)chunk = 16 * 1024with open(target_file_name, 'wb') as f:while True:chunk = response.read(chunk)if not chunk:breakf.write(chunk)?
源碼見blog.
轉(zhuǎn)載于:https://www.cnblogs.com/rkfeng/p/8366327.html
總結(jié)
以上是生活随笔為你收集整理的python下载大文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS 14 可以越狱了吗支持哪些设备
- 下一篇: python之字符串连接