2016-05-01 106 views
0

我正在使用praw从reddit线程中抓取信息。我可以使用r.get_submission(thread).comments在线索中给我所有的评论,但是现在我想遍历所有这些评论并获得孩子的评论。在Python中使用Praw获得孩子评论的Reddit

这是我有:

r = praw.Reddit(user_agent="archiver v 1.0") 
thread = "https://www.reddit.com/r/AskReddit/comments/4h4o7s/what_do_you_regret_doing_at_university/" 
r.login(settings['username'], settings['password'], disable_warning=True) 
submission = r.get_submission(thread) 

for comment in submission.comments: 
    #this works, prints out the comments text 
    print(comment.body) 

    #now i want to get the child comments that are replied to this comment 
    commentSubmission = r.get_submission(comment.permalink) 
    #ideally comments[0] should show me first reply, comments[1] the second. etc 
    print(commentSubmission.comments[1]) 

这将引发IndexError: list index out of range。我使用的试图得到尽可能提交注释的方法,因为它是类同的解决方案,我看到这里的时候,我是研究https://www.reddit.com/r/redditdev/comments/1kxd1n/how_can_i_get_the_replies_to_a_comment_with_praw/

我的问题是:给定一个praw comment对象,我怎么能遍历所有孩子的评论是答复?我想获得所有直接回复给另一个评论对象的评论。

例如,我在程序的示例线程,则第一个评论是Not going out freshman year我想得到这样Meh, I never went out at all in college.Your story sounds identical to mine

回答

2

响应评论这是一个简单的comment.replies,它返回同一种迭代的submission.commentsCommentMoreComments对象,后者在同一级别有更多评论。

一些示例代码:

submission = r.get_submission(thread) 
process_comments(submission.comments) 

def process_comments(objects): 
    for object in objects: 
     if type(object).__name__ == "Comment": 
      process_comments(object.replies) # Get replies of comment 

      # Do stuff with comment (object) 

     elif type(object).__name__ == "MoreComments": 
      process_comments(object.comments()) # Get more comments at same level 
相关问题