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