处理 PRAW 中的错误 (Reddit)
Handling Errors in PRAW (Reddit)
我正在尝试制作一个自动回复机器人来扫描 r/All 给定命令的提交评论,例如。 !命令
当它检测到此命令时,它会用字符串回复评论:
eg. "Hello"
我得到的错误是新帐户有限制,他们只能每 10 分钟发表一次评论。当机器人评论并移动到下一条评论回复它时,会收到此错误:
raise APIException(*errors[0])
praw.exceptions.APIException: RATELIMIT: 'you are doing that too much.
try again in 2 minutes.' on field 'ratelimit'
我怎样才能检测到这种类型的错误,以便代码知道该怎么做,目前整个脚本停止了,我必须在 10 分钟后再次 运行 它。
读过 https://praw.readthedocs.io/en/latest/code_overview/exceptions.html 但还是没读懂
这里提出了一个Exception
,你可以handle使用try..except
try:
the_api_call()
except APIException as e:
print "Handling exception like a baus"
P.S。您需要导入 APIException
,因为它不是内置异常。
正如 Samaksh Jain 所说,
使用 try..catch
使用以下
导入API异常
import django
from rest_framework.exceptions import APIException
我正在尝试制作一个自动回复机器人来扫描 r/All 给定命令的提交评论,例如。 !命令
当它检测到此命令时,它会用字符串回复评论:
eg. "Hello"
我得到的错误是新帐户有限制,他们只能每 10 分钟发表一次评论。当机器人评论并移动到下一条评论回复它时,会收到此错误:
raise APIException(*errors[0])
praw.exceptions.APIException: RATELIMIT: 'you are doing that too much.
try again in 2 minutes.' on field 'ratelimit'
我怎样才能检测到这种类型的错误,以便代码知道该怎么做,目前整个脚本停止了,我必须在 10 分钟后再次 运行 它。
读过 https://praw.readthedocs.io/en/latest/code_overview/exceptions.html 但还是没读懂
这里提出了一个Exception
,你可以handle使用try..except
try:
the_api_call()
except APIException as e:
print "Handling exception like a baus"
P.S。您需要导入 APIException
,因为它不是内置异常。
正如 Samaksh Jain 所说,
使用 try..catch
使用以下
导入API异常import django
from rest_framework.exceptions import APIException