【问题标题】:Python reddit API: efficiently parse all comments in a subredditPython reddit API:有效地解析 subreddit 中的所有评论
【发布时间】:2018-06-24 20:49:56
【问题描述】:

我正在尝试编写一个聊天机器人并让它扫描添加到其中的所有 cmets。

目前我通过每 X 秒扫描到最后 Y 个 cmets 来做到这一点:

handle = praw.Reddit(username=config.username,
                    password=config.password,
                    client_id=config.client_id,
                    client_secret=config.client_secret,
                    user_agent="cristiano corrector v0.1a")
while True:
    last_comments = handle.subreddit(subreddit).comments(limit=Y)
    for comment in last_comments:
        #process comments
    time.sleep(X)

我很不满意,因为可能有很多重叠(这可以通过跟踪 cmets id 来解决)并且一些 cmets 被扫描了两次,而另一些则被忽略了。使用此 API 是否有更好的方法?

【问题讨论】:

    标签: python praw


    【解决方案1】:

    我在 PRAW API 中找到了使用 stream 的解决方案。详情在https://praw.readthedocs.io/en/latest/tutorials/reply_bot.html

    在我的代码中:

    handle = praw.Reddit(username=config.username,
                        password=config.password,
                        client_id=config.client_id,
                        client_secret=config.client_secret,
                        user_agent="cristiano corrector v0.1a")
    
    for comment in handle.subreddit(subreddit).stream.comments():
        #process comments
    

    这应该会节省一些 CPU 和网络负载。

    【讨论】:

      猜你喜欢
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-14
      • 1970-01-01
      • 2014-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多