我想用python脚本下载很多文件,但是经常就有那么几个出错,写了个error handling,跳了过去,但是把出错的链接保存了一下。

转过天来,研究了一下出的什么错。

一个报错如下:

PS C:\temp> python .\DownloadFromList.py

Downloading https://github.com/Unity-Technologies/ScriptableRenderPipeline/archive/master.zip

Traceback (most recent call last):
   File ".\DownloadFromList.py", line 20, in <module>
     r = requests.get(url)
   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\api.py", line 72, in get
     return request('get', url, params=params, **kwargs)
   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\api.py", line 58, in request
     return session.request(method=method, url=url, **kwargs)
   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 512, in request
     resp = self.send(prep, **send_kwargs)
   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 644, in send
     history = [resp for resp in gen] if allow_redirects else []
   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 644, in <listcomp>
     history = [resp for resp in gen] if allow_redirects else []
   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 222, in resolve_redirects
     **adapter_kwargs
   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 662, in send
     r.content
   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\models.py", line 827, in content
     self._content = b''.join(self.iter_content(CONTENT_CHUNK_SIZE)) or b''

MemoryError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
   File ".\DownloadFromList.py", line 28, in <module>
     print("Error happened:", e.message)

AttributeError: 'MemoryError' object has no attribute 'message'

PS C:\temp>

上网搜索了一下, 找到了解决方案.

为了防止这个参考资料的网页消失(以前经常发生的), 所以我就直接把代码抄过来放在这里, 备用(抄袭,嗯,注明了出处就可以光明正大的抄袭).

使用request


def download_file(url):

local_filename = url.split('/')[-1]

# NOTE the stream=True parameter

r = requests.get(url, stream=True)

with open(local_filename, 'wb') as f:

for chunk in r.iter_content(chunk_size=1024):

if chunk: # filter out keep-alive new chunks

f.write(chunk)

f.flush()

return local_filename

使用urllib2


file = urllib2.urlopen('url')

with open('filename','w') as f:

while True:

tmp = file.read(1024)

if not tmp:

break

f.write(tmp)

参考资料

==================

https://ox0spy.github.io/post/python/python-download-large-file-without-out-of-memory/

参考资料所援引的代码来自下面的两个链接。

http://stackoverflow.com/questions/16694907/how-to-download-large-file-in-python-with-requests-py

http://stackoverflow.com/questions/27053028/how-to-download-large-file-without-memoryerror-in-python

如何Python下载大文件?的更多相关文章

  1. python下载大文件

    1. wget def download_big_file_with_wget(url, target_file_name): """ 使用wget下载大文件 Note: ...

  2. python 下载大文件

    当使用requests的get下载大文件/数据时,建议使用使用stream模式. 当把get函数的stream参数设置成False时,它会立即开始下载文件并放到内存中,如果文件过大,有可能导致内存不足 ...

  3. Python 下载超大文件

    使用python下载超大文件, 直接全部下载, 文件过大, 可能会造成内存不足, 这时候要使用requests 的 stream模式, 主要代码如下 iter_content:一块一块的遍历要下载的内 ...

  4. 转(Response.WriteFile 无法下载大文件解决方法)

    以前用Response.WriteFile(filename),但当遇到大文件时无法完整下载. 该方法最大的问题,它不是直接将数据抛到客户端,而是在服务器端(IIS)上缓存.当下载文件比较大时,服务器 ...

  5. 如何使用Python读取大文件

    背景 最近处理文本文档时(文件约2GB大小),出现memoryError错误和文件读取太慢的问题,后来找到了两种比较快Large File Reading 的方法,本文将介绍这两种读取方法. 准备工作 ...

  6. ASP.Net 下载大文件的实现

    当我们的网站需要支持下载大文件时,如果不做控制可能会导致用户在访问下载页面时发生无响应,使得浏览器崩溃.可以参考如下代码来避免这个问题. 关于此代码的几点说明: 1. 将数据分成较小的部分,然后将其移 ...

  7. Android 开发工具类 27_多线程下载大文件

    多线程下载大文件时序图 FileDownloader.java package com.wangjialin.internet.service.downloader; import java.io.F ...

  8. ASP.NET Core下载大文件的实现

    当我们的ASP.NET Core网站需要支持下载大文件时,如果不做控制可能会导致用户在访问下载页面时发生无响应,使得浏览器崩溃.可以参考如下代码来避免这个问题. 关于此代码的几点说明: 将数据分成较小 ...

  9. [libcurl]_[0基础]_[使用libcurl下载大文件]

    场景: 1. 在Windows编程时, 下载http页面(html,xml)能够使用winhttp库,可是并非非常下载文件,由于会失败. 由此引出了WinINet库,无奈这个库的稳定性比較低,使用样例 ...

随机推荐

  1. 从html页面中抽取table表格数据

    /** * [getDataFromTrElems 获取表格行元素数据] * @param {[Object]} trElems [trs dom] * @param {[String]} type ...

  2. 【docker】将容器中数据拷贝到主机

    参考:http://blog.csdn.net/yangzhenping/article/details/43667785 docker cp <containerId>:/file/pa ...

  3. laravel 同数据表字段比较查询和状态不正规排序

    今天写群组推荐接口,要求未满的群 ( 群最大人数字段maxusers, 群人数字段affiliations_count 都在群组表中),官方,热门(普通群0 ,官方1,热门2 ) 排序的群 同表字段比 ...

  4. unittest中更高效的执行测试用例一个类只需要打开一次浏览器

    示例代码 baidu.py # _*_ coding:utf-8 _*_ import csv,unittest #导入csv模块 from time import sleep from seleni ...

  5. 对象本田CRV

    <script> window.onload = function (ev) { var vcar = new Car("本田", "CRV", 4 ...

  6. Chakra调试笔记 TypedArray

    一.TypedArray类型 TypedArray是漏洞中常见到的结构,手册用法有四 1.new TypedArray(length); //byteLength=length * sizeof(Ty ...

  7. .NetCore下使用Prometheus实现系统监控和警报 (六)进阶Grafana集成自定义收集指标

    Prometheus中包含了很多收集指标,那么我们怎来在Grafana中来使用呢? 接下来我们还是以之前自定义的来演示如图:我们在Prometheus中已经可以看到这个之前我们自定义的类型了 关于Gr ...

  8. day--16页面布局

        后台页面布局 一.fixed布局 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  9. 多线程中使用CheckForIllegalCrossThreadCalls = false访问窗口

    在多线程程序中,新创建的线程不能访问UI线程创建的窗口控件,如果需要访问窗口中的控件,可以在窗口构造函数中将CheckForIllegalCrossThreadCalls设置为 false publi ...

  10. 【AtCoder】ARC081

    C - Make a Rectangle 每次取两个相同的且最大的边,取两次即可 #include <bits/stdc++.h> #define fi first #define se ...