gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 03:31:20 25 4
gpt4 key购买 nike

是否有任何方法可以限制 Reddit 上特定帖子的评论探索深度。我们有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 拆分它们并不那么复杂。

关于python - 在 PRAW 中探索特定深度的评论?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40906867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com