我正在使用praw从reddit线程中抓取信息。我可以使用r.get_submission(thread).comments
在一个帖子中给我所有的评论,但现在我想迭代所有这些评论并得到孩子的评论。
这就是我所拥有的:
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/时看到的解决方案类似
我的问题是:给定一个comment
对象,我如何遍历所有回复的子评论?我希望得到所有直接回复另一个评论对象的评论。
例如,在我的程序中的示例帖子中,第一个评论是Not going out freshman year
我想获得响应评论,例如Meh, I never went out at all in college.
和Your story sounds identical to mine
答案 0 :(得分:4)
这是一个简单的comment.replies
,它返回与submission.comments
和Comment
和MoreComments
对象相同的可迭代类型,后者可以在同一级别发表更多评论
一些示例代码:
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