如何修复 Python 中的 'During handling of the above exception, another exception occurred:' 错误?
How to fix 'During handling of the above exception, another exception occurred:' Error in Python?
我正在尝试创建一个 reddit 机器人来检查 subreddit 中的最新评论,如果评论包含错误引用,我希望机器人用实际引用来回复。我的问题是,由于 reddit 超时,我的机器人等待几分钟后,等待结束后,它抛出异常错误。
我试图通过创建一个 exc 变量并将其设置为 0 或 1 来让它一次只处理一个异常,但这没有用。
这是我的代码(不包括识别信息):
import praw
import re
import time
import os
reddit = praw.Reddit(client_id= 'id',
client_secret= 'secret',
user_agent='<console: reddit_bot: 0.0.1 (by /u/username)>',
username= 'username',
password= 'password'
)
if not os.path.isfile("comments_replied_to.txt"):
comments_replied_to = []
else:
with open("comments_replied_to.txt", "r") as f:
comments_replied_to = f.read()
comments_replied_to = comments_replied_to.split("\n")
comments_replied_to = filter(None, comments_replied_to)
subreddit = reddit.subreddit('subreddit')
pos=0
exc = 0
keywords = [ 'Luke, I am your father', 'Do you feel lucky, punk?']
for comment in subreddit.stream.comments():
for keyword in keywords:
if keyword in comment.body and comment.id not in comments_replied_to and comment.author != 'thatotteraccount':
print("String with " + keyword + " found in comment " + comment.id)
if keyword == 'Luke, I am your father':
if exc==0:
try:
comment.reply('* "No, I am your Father."')
except praw.exceptions.APIException as e:
exc=1
if (e.error_type == "RATELIMIT"):
delay = re.search("(\d+) minutes", e.message)
if delay:
delay_seconds = float(int(delay.group(1)) * 60)
time.sleep(delay_seconds)
comment.reply('* "No, I am your Father."')
exc=0
else:
delay = re.search("(\d+) seconds", e.message)
delay_seconds = float(delay.group(1))
time.sleep(delay_seconds)
comment.reply('* "No, I am your Father."')
exc=0
if keyword == 'Do you feel lucky, punk?':
if exc==0:
try:
comment.reply('* "Youve got to ask yourself one question: Do I feel lucky? Well, do ya punk?" ')
except praw.exceptions.APIException as e:
exc=1
if (e.error_type == "RATELIMIT"):
delay = re.search("(\d+) minutes?", e.message)
if delay:
delay_seconds = float(int(delay.group(1)) * 60)
time.sleep(delay_seconds)
comment.reply('* "Youve got to ask yourself one question: Do I feel lucky? Well, do ya punk?" ')
exc=0
else:
delay = re.search("(\d+) seconds", e.message)
delay_seconds = float(delay.group(1))
time.sleep(delay_seconds)
comment.reply('* "Youve got to ask yourself one question: Do I feel lucky? Well, do ya punk?" ')
exc=0
print("Replied to comment" + comment.id)
list(comments_replied_to).append(comment.id)
with open ("comments_replied_to.txt", "a") as f:
f.write(comment.id + "\n")
它抛出的错误是:
File "C:\Users\Blaze\Desktop\reddit_bot2.py", line 56, in <module>
comment.reply('* "Youve got to ask yourself one question: Do I feel lucky? Well, do ya punk?" ')
File "C:\Users\Blaze\AppData\Local\Programs\Python\Python37-32\lib\site-packages\praw\models\reddit\mixins\replyable.py", line 26, in reply
return self._reddit.post(API_PATH['comment'], data=data)[0]
File "C:\Users\Blaze\AppData\Local\Programs\Python\Python37-32\lib\site-packages\praw\reddit.py", line 483, in post
return self._objector.objectify(data)
File "C:\Users\Blaze\AppData\Local\Programs\Python\Python37-32\lib\site-packages\praw\objector.py", line 149, in objectify
raise APIException(*errors[0])
praw.exceptions.APIException: RATELIMIT: 'you are doing that too much. try again in 8 minutes.' on field 'ratelimit'
__During handling of the above exception, another exception occurred:__
Traceback (most recent call last):
File "C:\Users\Blaze\Desktop\reddit_bot2.py", line 65, in <module>
comment.reply('* "Youve got to ask yourself one question: Do I feel lucky? Well, do ya punk?" ')
File "C:\Users\Blaze\AppData\Local\Programs\Python\Python37-32\lib\site-packages\praw\models\reddit\mixins\replyable.py", line 26, in reply
return self._reddit.post(API_PATH['comment'], data=data)[0]
File "C:\Users\Blaze\AppData\Local\Programs\Python\Python37-32\lib\site-packages\praw\reddit.py", line 483, in post
return self._objector.objectify(data)
File "C:\Users\Blaze\AppData\Local\Programs\Python\Python37-32\lib\site-packages\praw\objector.py", line 149, in objectify
raise APIException(*errors[0])
praw.exceptions.APIException: RATELIMIT: 'you are doing that too much. try again in 6 seconds.' on field 'ratelimit'
感谢任何帮助,谢谢。
在我看来,您正在等待它告诉您等待的时间,但 Reddit 尚未完成冷却。您可能应该将睡眠时间增加 10-30 秒。
我正在尝试创建一个 reddit 机器人来检查 subreddit 中的最新评论,如果评论包含错误引用,我希望机器人用实际引用来回复。我的问题是,由于 reddit 超时,我的机器人等待几分钟后,等待结束后,它抛出异常错误。
我试图通过创建一个 exc 变量并将其设置为 0 或 1 来让它一次只处理一个异常,但这没有用。
这是我的代码(不包括识别信息):
import praw
import re
import time
import os
reddit = praw.Reddit(client_id= 'id',
client_secret= 'secret',
user_agent='<console: reddit_bot: 0.0.1 (by /u/username)>',
username= 'username',
password= 'password'
)
if not os.path.isfile("comments_replied_to.txt"):
comments_replied_to = []
else:
with open("comments_replied_to.txt", "r") as f:
comments_replied_to = f.read()
comments_replied_to = comments_replied_to.split("\n")
comments_replied_to = filter(None, comments_replied_to)
subreddit = reddit.subreddit('subreddit')
pos=0
exc = 0
keywords = [ 'Luke, I am your father', 'Do you feel lucky, punk?']
for comment in subreddit.stream.comments():
for keyword in keywords:
if keyword in comment.body and comment.id not in comments_replied_to and comment.author != 'thatotteraccount':
print("String with " + keyword + " found in comment " + comment.id)
if keyword == 'Luke, I am your father':
if exc==0:
try:
comment.reply('* "No, I am your Father."')
except praw.exceptions.APIException as e:
exc=1
if (e.error_type == "RATELIMIT"):
delay = re.search("(\d+) minutes", e.message)
if delay:
delay_seconds = float(int(delay.group(1)) * 60)
time.sleep(delay_seconds)
comment.reply('* "No, I am your Father."')
exc=0
else:
delay = re.search("(\d+) seconds", e.message)
delay_seconds = float(delay.group(1))
time.sleep(delay_seconds)
comment.reply('* "No, I am your Father."')
exc=0
if keyword == 'Do you feel lucky, punk?':
if exc==0:
try:
comment.reply('* "Youve got to ask yourself one question: Do I feel lucky? Well, do ya punk?" ')
except praw.exceptions.APIException as e:
exc=1
if (e.error_type == "RATELIMIT"):
delay = re.search("(\d+) minutes?", e.message)
if delay:
delay_seconds = float(int(delay.group(1)) * 60)
time.sleep(delay_seconds)
comment.reply('* "Youve got to ask yourself one question: Do I feel lucky? Well, do ya punk?" ')
exc=0
else:
delay = re.search("(\d+) seconds", e.message)
delay_seconds = float(delay.group(1))
time.sleep(delay_seconds)
comment.reply('* "Youve got to ask yourself one question: Do I feel lucky? Well, do ya punk?" ')
exc=0
print("Replied to comment" + comment.id)
list(comments_replied_to).append(comment.id)
with open ("comments_replied_to.txt", "a") as f:
f.write(comment.id + "\n")
它抛出的错误是:
File "C:\Users\Blaze\Desktop\reddit_bot2.py", line 56, in <module>
comment.reply('* "Youve got to ask yourself one question: Do I feel lucky? Well, do ya punk?" ')
File "C:\Users\Blaze\AppData\Local\Programs\Python\Python37-32\lib\site-packages\praw\models\reddit\mixins\replyable.py", line 26, in reply
return self._reddit.post(API_PATH['comment'], data=data)[0]
File "C:\Users\Blaze\AppData\Local\Programs\Python\Python37-32\lib\site-packages\praw\reddit.py", line 483, in post
return self._objector.objectify(data)
File "C:\Users\Blaze\AppData\Local\Programs\Python\Python37-32\lib\site-packages\praw\objector.py", line 149, in objectify
raise APIException(*errors[0])
praw.exceptions.APIException: RATELIMIT: 'you are doing that too much. try again in 8 minutes.' on field 'ratelimit'
__During handling of the above exception, another exception occurred:__
Traceback (most recent call last):
File "C:\Users\Blaze\Desktop\reddit_bot2.py", line 65, in <module>
comment.reply('* "Youve got to ask yourself one question: Do I feel lucky? Well, do ya punk?" ')
File "C:\Users\Blaze\AppData\Local\Programs\Python\Python37-32\lib\site-packages\praw\models\reddit\mixins\replyable.py", line 26, in reply
return self._reddit.post(API_PATH['comment'], data=data)[0]
File "C:\Users\Blaze\AppData\Local\Programs\Python\Python37-32\lib\site-packages\praw\reddit.py", line 483, in post
return self._objector.objectify(data)
File "C:\Users\Blaze\AppData\Local\Programs\Python\Python37-32\lib\site-packages\praw\objector.py", line 149, in objectify
raise APIException(*errors[0])
praw.exceptions.APIException: RATELIMIT: 'you are doing that too much. try again in 6 seconds.' on field 'ratelimit'
感谢任何帮助,谢谢。
在我看来,您正在等待它告诉您等待的时间,但 Reddit 尚未完成冷却。您可能应该将睡眠时间增加 10-30 秒。