Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在PRAW中探索到特定深度的评论?_Python_Praw_Reddit - Fatal编程技术网

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

Python 在PRAW中探索到特定深度的评论?,python,praw,reddit,Python,Praw,Reddit,有没有办法限制对reddit上特定帖子评论的深度探索。我们有replace_more_注释,它试图替换尽可能多的_注释,但我们可以限制这种扩展。或者我需要在这些评论上编写自己的dfs版本吗 谢谢因为你提到了替换更多的评论我想你说的是PRAW 3.5 遗憾的是,PRAW没有以comment.depth的形式提供信息。它实际上并没有把这些信息保存在任何地方 如果您希望获得一组深度较低的注释(如仅限第一级和第二级注释),则可以在没有dfs或bfs的情况下进行 submission.replace_mo

有没有办法限制对reddit上特定帖子评论的深度探索。我们有replace_more_注释,它试图替换尽可能多的_注释,但我们可以限制这种扩展。或者我需要在这些评论上编写自己的dfs版本吗


谢谢

因为你提到了
替换更多的评论
我想你说的是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)
如果您想要非固定深度,那么您只需要自己的实现。但由于注释是如何设置和从RedditAPI检索的,所以您应该使用bfs而不是dfs

还有另一种方法,可以在Praw4.0中使用(昨天发布)。是我指的文档的特定部分:

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)
虽然能够进行自己的广度优先遍历非常棒,但CommentForest提供了一个方便的方法list(),它返回按与上面代码相同的顺序遍历的注释列表。因此,上述内容可以改写为:

从这里你会收到一个评论列表,以便bfs给你

[first_level_comment, first_level_comment, first_level_comment, second_level_comment, 
second_level_comment, third_level_comment, ...]
在这种情况下,基于ID和父\u ID拆分这些内容并不复杂

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