Python 我的reddit机器人一遍又一遍地回复相同的评论和自己
因此,我对python非常陌生,并制作了一个简单的reddit机器人,用于回复评论。今天早上它运行得很好,但现在它一遍又一遍地回复同样的评论甚至自己。 我找不到如何用我糟糕的谷歌搜索技巧解决这个问题。。。所以,我来了。 代码是这样的Python 我的reddit机器人一遍又一遍地回复相同的评论和自己,python,python-3.x,windows,reddit,praw,Python,Python 3.x,Windows,Reddit,Praw,因此,我对python非常陌生,并制作了一个简单的reddit机器人,用于回复评论。今天早上它运行得很好,但现在它一遍又一遍地回复同样的评论甚至自己。 我找不到如何用我糟糕的谷歌搜索技巧解决这个问题。。。所以,我来了。 代码是这样的 import praw import time import config REPLY_MESSAGE = "Di Molto indeed" def authenticate(): print("Authenticating...") redd
import praw
import time
import config
REPLY_MESSAGE = "Di Molto indeed"
def authenticate():
print("Authenticating...")
reddit = praw.Reddit(client_id = config.client_id,
client_secret = config.client_secret,
username = config.username,
password = config.password,
user_agent = 'FuriousVanezianLad by /u/FuriousVanezianLad')
print("Authenticated as {}".format(reddit.user.me()))
return reddit
def main():
reddit = authenticate()
while True:
run_bot(reddit)
def run_bot(reddit):
print("Obtaining 25 comments...")
for comment in reddit.subreddit('test').comments(limit=25):
if "Di Molto" in comment.body:
print('String with "Di Molto" found in comment {}',format(comment.id))
comment.reply(REPLY_MESSAGE)
print("Replied to comment " + comment.id)
print("Sleeping for 10 seconds...")
time.sleep(10)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print("Interrupted")
我从Bboe的更新中获取了这段代码,内容是“如何制作reddit机器人-Busterroni的第一部分”
我不知道出了什么问题,但它会自言自语。
对不起,我知道这是一个愚蠢的问题,它可能会解决之前,但我找不到它
再三抱歉
提前感谢您的帮助 问题在于,您一次又一次地获取最新的25条评论,而没有新的评论(或者评论速度很慢),因此您最终会重复处理相同的评论 我建议改为使用流,这是一个PRAW特性。流在发布时获取新项目(在本例中为注释)。这样,您将不会多次处理同一条注释。另外,在回复之前,请检查您是否发表了特定的评论。下面是代码的修改版本,它使用流并检查您是否发表了评论:
def run_bot(reddit):
me = reddit.user.me()
try:
for comment in reddit.subreddit('test').stream.comments(skip_existing=True):
if "Di Molto" in comment.body and comment.author != me:
print('String with "Di Molto" found in comment {}',format(comment.id))
comment.reply(REPLY_MESSAGE)
print("Replied to comment " + comment.id)
except Exception as e:
print("Got exception: {}".format(e))
print("Sleeping for 10 seconds...")
time.sleep(10)
没有新的评论吗?这段代码就是这么做的……它总是会得到最后25条注释……在睡眠10天后……如果没有新的注释,它会回复sameIs。有一种方法可以捕获注释作者(可能是comment.author)并进行检查,比如,
if comment.author=='bot name':return
@vka除了它自己的注释之外,没有新的注释。。。那么在睡了10天后,它会在同一个帖子上发表评论吗?我该如何解决这个问题?