I can access a subreddit with this code:
hot = praw.Reddit(...).subreddit("AskReddit").hot(limit=10)
for post in hot:
print(post.title, post.url)
Would you watch a show where a billionaire CEO has to go an entire month on their lowest paid employees salary, without access to any other resources than that of the employee? What do you think would happen? https://www.reddit.com/r/AskReddit/comments/f08dxb/would_you_watch_a_show_where_a_billionaire_ceo/
All of the subreddits are invited to a house party. What kind of stuff goes down? https://www.reddit.com/r/AskReddit/comments/f04t6o/all_of_the_subreddits_are_invited_to_a_house/
How can I get the comments of a particular submission, for example the first one:
https://www.reddit.com/r/AskReddit/comments/f08dxb/would_you_watch_a_show_where_a_billionaire_ceo/
PRAW has a section in the documentation that answers this question. See Comment Extraction and Parsing: Extracting comments with PRAW.
Modifying your code based on the linked documentation yields
from praw.models import MoreComments
reddit = praw.Reddit(...)
hot = reddit.subreddit("AskReddit").hot(limit=10)
for submission in hot:
print(submission.title)
for top_level_comment in submission.comments:
if isinstance(top_level_comment, MoreComments):
continue
print(top_level_comment.body)
This will print all of the top-level comments on the submission. Note that the Comment
class has other attributes, many of which are documented here. For example, to print some of the attributes of a comment
that you circled in red, try:
print(comment.author)
print(comment.score)
print(comment.created_utc) # as a Unix timestamp
print(comment.body)
As the linked documentation suggests, you can get every comment in the submission using the .list()
method:
reddit = praw.Reddit(...)
hot = reddit.subreddit("AskReddit").hot(limit=10)
for submission in hot:
print(submission.title)
submission.comments.replace_more(limit=None)
for comment in submission.comments.list():
print(comment.author)
print(comment.score)
print(comment.created_utc) # as a Unix timestamp
print(comment.body)
Thank you. I've already found this with the link in your previous message. A bit hairy tree logic structure.
Yes. It reflects the interface on Reddit. Most comments have some number (possibly zero) of replies, and in addition may or may not have a "more comments" button. If you want to traverse the tree manually by using a stack/queue or recursion, that's possible. But also the
.list()
method is helpful to just get all of the comments, if that's what you want.