Python脚本(Reddit bot)重置http错误/超时上的变量

时间:2014-03-22 04:58:22

标签: python bots reddit reconnect

要提前......我还是Python的新手。我有一个reddit机器人,它在一个subreddit中读取一个指定的帖子。根据响应的语法,它回复并存储变量(例如,评论者姓名,增量号码,已经显示)。

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

此外,我通过暂时断开互联网测试了这一点。一旦它重新连接机器人做同样的事情。

有没有办法可以让机器人等待并且还记得它已经看到并回复了预先存在的帖子?

非常感谢您提供的任何帮助,谢谢。

对于我所有的搜索和浏览,这与我想出的一样多(再次,我对此不熟悉):

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

2 个答案:

答案 0 :(得分:1)

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

psedo代码:

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

  sleep(5)

答案 1 :(得分:0)

如果您想让它记住帖子,那么您可以在回复/看到用户发布后将其添加到您的代码中

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:
    ...