我想用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. Coursera台大机器学习技法课程笔记07-Blending and Bagging

    这一节讲如何将得到的feature或hypothesis组合起来用于预测. 1. 林老师给出了几种方法 在选择g时,需要选择一个很强的g来确保Eval最小,但如果每个g都很弱该怎么办呢 这个时候可以选 ...

  2. cf1061c 普通dp题

    题解见https://blog.csdn.net/godleaf/article/details/84402128 这一类dp题是可以压缩掉一维空间的,本题枚举a1到an,枚举到ai时枚举ai的每个约 ...

  3. hdu1890 splay维护区间翻转

    这题的建模有点不太一样,是按结点横坐标赋予键值的 同时每次rotate和splay时都要注意下往上往下更新 /* 先建立好splay tree,将结点按num/输入顺序排序,遍历时每次将当前结点提到根 ...

  4. pytest八:skip 跳过用例

    这是一个快速指南,介绍如何在不同情况下跳过模块中的测试1.无条件地跳过模块中的所有测试:pytestmark = pytest.mark.skip("all tests still WIP& ...

  5. python简单笔记

    Remarks:python中注意缩进(Tab键或者4个空格) print(输出) 格式:print(values) 字符串.数字.变量等都可以输出: 实例: print(1)->1 print ...

  6. 我靠,上班eclipse看糗事百科

    package test; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; ...

  7. 使用vmware提示无法打开内核设备 \\.\Global\vmx86: 系统找不到指定的文件

    问题描述 打开虚拟机时候提示 “vmware没有正常关闭,再次打开使用时蓝屏,在安全模式下再次打开不会蓝屏,但提示“无法打开内核设备 \\.\Global\vmx86: 系统找不到指定的文件,你想要安 ...

  8. POJ 2139 Six Degrees of Cowvin Bacon (Floyd)

    题意:如果两头牛在同一部电影中出现过,那么这两头牛的度就为1, 如果这两头牛a,b没有在同一部电影中出现过,但a,b分别与c在同一部电影中出现过,那么a,b的度为2.以此类推,a与b之间有n头媒介牛, ...

  9. Codeforces 1041F Ray in the tube (看题解)

    Ray in the tube 感觉是套路题.. 如果确定一个差值x我们如何取确定答案呢, 我们把a[ i ] -> a[ i ] % (2 * x), 把b[ i ] -> (b[ i ...

  10. BZOJ1968 [Ahoi2005]COMMON 约数研究 数论

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1968 题意概括 求 ΣF(i)   (1<=i<=n)N<=1000000 F( ...