使用python下载超大文件, 直接全部下载, 文件过大, 可能会造成内存不足, 这时候要使用requests 的 stream模式,

主要代码如下

iter_content:一块一块的遍历要下载的内容
iter_lines:一行一行的遍历要下载的内容

def download_file(url, file_pname, chunk_size=1024*4):
"""
url: file url
file_pname: file save path
chunk_size: chunk size
"""
# 第一种
response_data_file = requests.get(url, stream=True)
with open(file_pname, 'wb') as f:
for chunk in response_data_file.iter_content(chunk_size=chunk_size):
if chunk:
f.write(chunk) # 第二种
with requests.get(url, stream=True) as req:
with open(file_pname, 'wb') as f:
for chunk in req.iter_content(chunk_size=chunk_size):
if chunk:
f.write(chunk)

python实现文件下载图片视频

如有错误欢迎指出

最新文章

  1. OneDrive无法正常登录
  2. c++ 类模版、成员函数模版、函数模版 用法
  3. Web Service实例——天气预报
  4. jquery.tablesorter.js 学习笔记
  5. 使用apache反向代理tomacat
  6. RESTClient
  7. freemarker写select组件报错总结(三)
  8. 线程同步Volatile与Synchronized(一)
  9. sqlserver修改计算机名称。
  10. C# 之 HttpRequest 类
  11. PostMessage 解析
  12. aspose.cells 插入图片
  13. html 统一资源定位器(url)和url编码
  14. [PHP] 05 - Cookie & Session
  15. HTML5 简单归纳 -- 前端知识 (一)
  16. 缩点tarjan
  17. MongoDB存储基础教程
  18. Python常用time处理
  19. android activity 启动模式
  20. 子类化GetOpenFileName/GetSaveFileName, 以及钩子函数OFNHookProc的使用的简要说明

热门文章

  1. selenium中的元素操作之三大等待(一)
  2. python 代码中log表示含义
  3. RStudio中安装factoextra包的问题
  4. MySQL语法顺序及执行顺序
  5. js 数据类型检测
  6. springCloud学习3(Netflix Hystrix弹性客户端)
  7. 纯css更改图片颜色的技巧
  8. JAVA基础之HttpServletResponse响应
  9. SpringBoot 传入JSON对象参数
  10. 图说jdk1.8新特性(1)--- 函数式接口