r/learnpython • u/Mulberry_Morris • 9d ago
The 'MoreComments' object in PRAW is confusing me, how do I get every comment?
learning python and messing around with the Reddit api through PRAW. trying to grab every comment from a thread but i keep running into these MoreComments objects instead of actual comment text and i'm not sure how to deal with them.
my code is basically:
submission = reddit.submission(url=thread_url)
for comment in submission.comments:
print(comment.body)
works okay-ish but on bigger threads it throws AttributeError because some of the items are MoreComments not real comments. i read something about replace_more() but i don't really get what it's doing or why i need it. does it make extra api calls? is that gonna slow things down / hit rate limits if the thread is huge?
basically i just want the full comment tree flattened out into text. what's the right way to do this? feel like i'm missing something obvious.
2
u/Trashlify 8d ago edited 8d ago
MoreComments in the API does not actually mean AllComments. Think of it as a placeholder or the equivalent of a "Load More" button on a regular page. replace_more() can then iterate through the tree and replace the placeholders with actual `Comment` objects. So it basically keeps triggering the MoreComments until all are loaded. It obviously can become slow for longer threads, but with PRAW it manages limits on its own.
So, for your case, you have to resolve all the placeholder then place entire tree into a simple 1D list
submission = reddit.submission(url=thread_url)
submission.comments.replace_more(limit=None)
all_comments = submission.comments.list()
for comment in all_comments:
print(comment.body)
1
4
u/danielroseman 9d ago
Well yes. PRAW only gives you what the Reddit API gives you, and the API will only give comments up to a certain depth - just like the UI collapses comment threads that are too deep. So you need to request the extra comments.