Python &引用;“内存不足”;机械化错误

Python &引用;“内存不足”;机械化错误,python,memory,urllib2,mechanize,Python,Memory,Urllib2,Mechanize,我试图一页一页地从网站上获取一些信息,基本上我是这样做的: import mechanize MechBrowser = mechanize.Browser() Counter = 0 while Counter < 5000: Response = MechBrowser.open("http://example.com/page" + str(Counter)) Html = Response.read() Response.close() Out

我试图一页一页地从网站上获取一些信息,基本上我是这样做的:

import mechanize
MechBrowser = mechanize.Browser()

Counter = 0

while Counter < 5000:
    Response = MechBrowser.open("http://example.com/page" + str(Counter))
    Html = Response.read()
    Response.close()

    OutputFile = open("Output.txt", "a")
    OutputFile.write(Html)
    OutputFile.close()

    Counter = Counter + 1
导入机械化
MechBrowser=mechanize.Browser()
计数器=0
当计数器<5000时:
响应=浏览器打开(“http://example.com/page“+str(柜台))
Html=Response.read()
答复:close()
OutputFile=open(“Output.txt”、“a”)
OutputFile.write(Html)
OutputFile.close()
计数器=计数器+1
好的,上面的代码最终抛出了“内存不足”错误,在任务管理器中,它显示脚本在运行几个小时后消耗了将近1GB的内存。。。为什么


谁能告诉我出了什么问题吗?

这不完全是内存泄漏,而是一个未记录的功能。基本上,
mechanize.Browser()


如果在
Response.close()
之后添加对
MechBrowser.clear_history()
的调用,应该可以解决问题。

我要尝试排除的第一件事是mechanize正在泄漏内存——只需使用
urlib2.urlopen()
即可。注意,对于范围内的计数器(5000):
,使用
更像pythonic。PEP8建议变量名使用小写字母和下划线,而CamelCase是为类保留的。此外,请使用
查看文件的写入/读取功能。(a)这种“我的代码怎么了”的问题在codereview上更好(b)变量重用本身不是问题(c)了解内存配置。@Marcin不,code review是用于工作代码的,这显然不起作用,因为它会引发异常。对于代码审查来说,这是一个离题的话题。