Python脚本(Reddit bot)重置http error/timeou上的变量

2024-04-19 22:18:53 发布

您现在位置:Python中文网/ 问答频道 /正文

首先…我对Python还是很陌生的。我有一个reddit机器人,它在我控制的subreddit中读取一个指定的帖子。根据响应的语法,它响应并存储变量(例如,注释者名称,一个递增的数字,已经看到)。你知道吗

如果reddit超时或出现500/504错误,我的机器人会重置变量。下一次成功加载帖子时,它会将所有用户评论视为新的,并再次响应它们。你知道吗

此外,我还通过暂时断开互联网连接来测试这一点。一旦重新连接,机器人就会做同样的事情。你知道吗

有没有一种方法可以让bot等待并仍然记得它已经看到并回复了已有的帖子?你知道吗

非常感谢您的帮助。你知道吗

对于我所有的搜索和浏览,这是我已经想出的(再次,我是新的这个):

except(IOError):
    time.sleep(30)
    pass

Tags: 方法用户名称错误语法评论机器人互联网
2条回答

一种方法是用try-catch块包装internet访问代码。如果页面获取失败,则跳过当前循环并继续下一次迭代

psedo代码:

while True:
  content = None
  try:
    content = fetch_content # might have error
  except:
    continue
  # do reply logic

  sleep(5)

如果您试图让它记住帖子,那么您可以在它回复/看到用户帖子后将其添加到代码的某个地方

posts = dict()
# some code reading or replying to the comments
posts.setdefault(user, []).append(post) # get posts[user] if it exists, if not create it with a list value, then append the post.
# some of your other code to check if it's in the dictionary

比如说也许吧

for post in posts:
    ...

相关问题 更多 >