“Out of Memory” error with mechanize

最后都变了- 提交于 2019-12-07 06:43:33

问题


I was trying to scrape some information from a website page by page, basically here's what I did:

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

Well, the above codes ended up throwing out "Out of Memory" error and in task manager it shows that the script used up almost 1GB memory after several hours running... how come?!

Would anybody tell me what went wrong?


回答1:


This is not exactly a memory leak, but rather an undocumented feature. Basically, mechanize.Browser() is collectively storing all browser history in memory as it goes.

If you add a call to MechBrowser.clear_history() after Response.close(), it should resolve the problem.



来源:https://stackoverflow.com/questions/9314149/out-of-memory-error-with-mechanize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!