1

我正在尝试运行该程序,但过了一会儿我收到“内存不足”错误。不知何故,我需要释放一些内存,但我不知道如何在 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()
4

1 回答 1

2

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

于 2013-06-16T16:55:05.997 回答