Python,使用mechanize时出现“Out of memory”错误

时间:2013-06-16 16:50:05

标签: python mechanize

我正在尝试运行此程序,但过了一段时间后,我收到“Out of memory”错误。不知何故,我需要释放一些内存,我不知道如何在Python中做到这一点。有人可以帮帮我吗?

非常感谢提前。

import re
import mechanize

br = mechanize.Browser()

print('trying to login')
br.open('http://www.erepublik.com/en')
br.select_form(nr=0)
br["citizen_email"]="xxxxx"
br["citizen_password"]="xxxxx"
response = br.submit()
html = response.read()
if html.find('Logout') == -1:
    print 'username and password mismatch'
else:
    print 'logged in successfully'

for i in range(6395674, 1000000, -1):
    print('fetching %d' % i)
    usrlink = 'http://www.erepublik.com/en/citizen/profile/%d' % i
    try:
        response = br.open(usrlink)
    except:
        print 'User not found'
    html_user = response.read()
    response.close()
br.clear_history()
    if html_user.find('Press director') == -1:
        print "user doesn't have newspaper"
    else:
        npl = br.follow_link(url_regex='/en/newspaper/', nr=0)
        html_news = npl.read()
        pos = html_news.find('<em class="subscribers">')
        if pos == -1:
            print 'Something went wrong during extracting sub number'
        else:
            all_int = re.findall(r'\d+', html_news[pos:])
            sub_num = all_int[0]
            msglink = "http://www.erepublik.com/en/main/messages-compose/%d" % i
            br.open(msglink)
            br.select_form(nr=1)
            br["citizen_subject"]="xxxxx"
            br["citizen_message"]="xxxxx" % sub_num
            response2 = br.submit()
        html_res_mess = response2.read()
        response2.close()
        br.clear_history()

1 个答案:

答案 0 :(得分:2)

range(6395674, 1000000, -1)创建了一个巨大的列表。对于迭代,您可以更好地使用xrange(6395674, 1000000, -1),这会创建一个惰性迭代(在实际需要时它只生成下一个数字)。我认为这也可以解决你的记忆问题。