探索 PRAW 中特定深度的评论?

Exploring comments up to a particular depth in PRAW?

有什么方法可以限制对 reddit 上特定 post 评论的探索深度。我们有 replace_more_comments 试图替换尽可能多的 more_comments 但我们可以限制这种扩展吗?或者我需要在这些评论上写我自己的 dfs 版本吗?

谢谢

既然你提到了 replace_more_comments 我假设你在谈论 PRAW 3.5。

遗憾的是,PRAW 不提供 comment.depth 形式的信息。它实际上不会在任何地方保留此信息。

如果你想获得一组较低的评论深度(比如只有一级和二级评论),那么你可以不用 dfs 或 bfs。

submission.replace_more_comments(limit=None,threshold=0)
for top_level_comment in submission.comments:
    for second_level_comment in top_level_comment.replies:
        print(second_level_comment.body)

如果您想要非固定深度,那么您只能使用自己的实现。但是由于评论的设置和从 reddit 检索的方式 api 你应该使用 bfs 而不是 dfs。

还有一种方法,就是PRAW 4.0(昨天发布)中的方法。 Here 是我所指的文档的特定部分:

submission.comments.replace_more(limit=0)
comment_queue = submission.comments[:]  # Seed with top-level
while comment_queue:
    comment = comment_queue.pop(0)
    print(comment.body)
    comment_queue.extend(comment.replies)

While it is awesome to be able to do your own breadth-first traversals, CommentForest provides a convenience method, list(), which returns a list of comments traversed in the same order as the code above. Thus the above can be rewritten as:

submission.comments.replace_more(limit=0)
for comment in submission.comments.list():
    print(comment.body)

从这里您会收到一份评论列表,以便 bfs 给您。

[first_level_comment, first_level_comment, first_level_comment, second_level_comment, 
second_level_comment, third_level_comment, ...]

在这种情况下,根据 ids 和 parent_ids.

拆分它们并不复杂