我和我的朋友正在用python进行一个项目。我们编写了一些代码,让他在收件箱中发送的每封电子邮件都在列表中搜索,如果他在列表中找到了,脚本就会在网站上执行一些操作。
问题是,当我们在一段时间后运行脚本时,我们得到了堆栈的递归错误!
我们通过添加sys.setrecursionlimit(sys.getrecursionlimit() + 100)
的方式修改了增加的代码,但在许多小时后,脚本给出:内存错误!堆栈溢出
这是整个脚本代码:
import zmail
import time
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
server = zmail.server('EMAIL', 'PASSWORD')
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)
sheet = client.open("SHEET LIST").sheet1
#sheet.update_cell(1, 6, "Error!")
def check():
sair = False
mailbox_info = server.stat()
sys.setrecursionlimit(sys.getrecursionlimit() + 100)
if mailbox_info[0] > 0:
total = mailbox_info[0]
for x in range(0, total):
#print(x)
body = server.get_mail(total - x)
body = body['content_text']
body = str(body)
print(body)
row = len(sheet.col_values(1))
emails = sheet.col_values(1)
passwords = sheet.col_values(2)
keys = sheet.col_values(4)
contador = 0
while sair == False:
#print(row)
#print(contador)
if body.find(str(emails[contador])) != -1:
#print("if")
f = open("log.txt", "a")
email = str(emails[contador])
passw = str(passwords[contador])
key = str(keys[contador])
print("Found this email " + email)
f.write("Found this email " + email + "\n")
driver = webdriver.Chrome(options=options)
try:
driver.get("WEBSITE FOR DOING ACTION")
except:
sheet.update_cell(contador + 1, 6, "Error!")
sair = True
email_in = driver.find_element_by_xpath("/html/body/div/div[2]/div/div[3]/input")
email_in.send_keys(email)
print("Writing email...")
f.write("Writing email... \n")
password_in = driver.find_element_by_xpath("/html/body/div/div[2]/div/div[4]/input")
password_in.send_keys(passw)
print("Writing password...")
f.write("Writing password... \n")
key_in = driver.find_element_by_xpath("/html/body/div/div[2]/div/div[6]/input")
key_in.send_keys(key)
print("Writing key...")
f.write("Writing key... \n")
checkbox_in = driver.find_element_by_xpath("/html/body/div/div[2]/div/div[2]/label[2]/input")
driver.execute_script("arguments[0].click();", checkbox_in)
print("Selecting checkbox...")
f.write("Selecting checkbox... \n")
button_in = driver.find_element_by_xpath("/html/body/div/div[2]/div/div[8]")
print("Clicking button...")
f.write("Clicking button... \n")
driver.execute_script("arguments[0].click();", button_in)
time.sleep(120)
if driver.find_element_by_xpath("/html/body/div/div[2]/div/div[1]/p[2]").is_displayed():
sheet.update_cell(contador + 1, 6, "Success!")
else:
sheet.update_cell(contador + 1, 6, "Error!")
driver.close()
contador += 1
if contador >= row:
sair = True
#sair = True
else:
#print("else")
contador += 1
if contador >= row:
sair = True
contador = 0
timeC()
else:
timeC()
def timeC():
time.sleep(10) ```
但我知道错误就在这部分代码中
timeC()
else:
timeC()
def timeC():
time.sleep(10)
check()
timeC()
我们如何解决男生问题?
这是递归限制超出检查范围的回溯:
File "main.py", line 100, in timeC
check()
File "main.py", line 95, in check
timeC()
File "main.py", line 100, in timeC
check()
File "main.py", line 95, in check
timeC()
File "main.py", line 100, in timeC
check()
File "main.py", line 95, in check
timeC()
File "main.py", line 100, in timeC
check()
File "main.py", line 95, in check
timeC()
File "main.py", line 100, in timeC
check()
File "main.py", line 95, in check
timeC()
File "main.py", line 100, in timeC
check()
File "main.py", line 95, in check
timeC()
File "main.py", line 100, in timeC
check()
File "main.py", line 25, in check
mailbox_info = server.stat()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\zmail\server.py", line 129, in stat
with self.pop_server as server:
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\zmail\abc.py", line 78, in __enter__
self.login()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\zmail\server.py", line 306, in login
self._make_server()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\zmail\server.py", line 290, in _make_server
self.server = poplib.POP3_SSL(self.host, self.port, timeout=self.timeout)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\poplib.py", line 447, in __init__
POP3.__init__(self, host, port, timeout)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\poplib.py", line 102, in __init__
self.sock = self._create_socket(timeout)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\poplib.py", line 450, in _create_socket
sock = POP3._create_socket(self, timeout)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\poplib.py", line 108, in _create_socket
return socket.create_connection((self.host, self.port), timeout)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 707, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 748, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
RecursionError: encoding with 'idna' codec failed (RecursionError: maximum recursion depth exceeded in comparison)
你有两个问题:
timeC
函数是罪魁祸首,因为它递归调用check
,可能类似于:check
之外执行一个简单的循环。摆脱timeC
和对它的调用,然后执行以下操作:在
check
内部,检查完成后,return
而不是进行递归调用。让外部循环负责安排下一次检查相关问题 更多 >
编程相关推荐