【发布时间】:2019-02-02 12:10:12
【问题描述】:
不久前我开始使用 python,我为 Reddit 制作了一个简单的机器人。不幸的是,该机器人的功能仅适用于在该机器人在终端中运行之前发布的帖子。如果在机器人运行后向 subreddit 发布帖子,机器人不会评论或获取这些帖子。
另一件可能相关的事情是控制台输出“目前没有适用的帖子”。两次而不是一次。我不确定这是否相关。
这是我认为对此问题重要的代码,如果您需要更多上下文,请告诉我!:
def mainloop():
counter1 = 0 # Counts submissions in new that have been crawled
for submission in subreddit.new(limit=5): # Get the 5 newest submissions
counter1 = counter1 + 1
callings = ['canada', 'canadian', '????????'] # Triggers
normalized_title = submission.title.lower()
normalized_text = submission.selftext.lower()
while True:
if submission.id not in posts_replied_to: # If the post is new to the bot
time.sleep(30) # Keep spam low
for canadian_mentions in callings:
if canadian_mentions in normalized_title: # If trigger is in title
# Make the reply, print to console, then add the post to the replied storage
submission.reply(reply_text)
print("Bot replying to : ", submission.title, "\n")
posts_replied_to.append(submission.id)
elif canadian_mentions in normalized_text: # If trigger is in text body
# Make the reply, print to console, then add the post to the replied storage
submission.reply(reply_text)
print("Bot replying to : ", submission.selftext, "\n")
posts_replied_to.append(submission.id)
else:
print("No applicable posts right now.")
【问题讨论】:
-
你有一个无限循环
while True:检查新帖子后:for submission in subreddit.new(limit=5):你进入无限循环后将永远不会再有新帖子。你需要重新排列你的代码。 -
非常感谢,问题是这个,以及另一件事。删除 while 循环并没有改变它没有获得新帖子的事实,但它是问题的一半。我打印新帖子的代码没有
else:语句,并且程序在该语句处处于空闲状态。如果我解决了这个问题并离开了while True循环,它仍然会有同样的问题!谢谢!
标签: python python-3.x bots reddit praw