我可以使用此代码访问subreddit:
hot = praw.Reddit(...).subreddit("AskReddit").hot(limit=10)
for i in hot:
print(i.title, i.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/
如何获得特定主题的内容,例如第一个主题:https://www.reddit.com/r/AskReddit/comments/f08dxb/would_you_watch_a_show_where_a_billionaire_ceo/
PRAW在documentation中有一个部分回答了这个问题。参见Comment Extraction and Parsing: Extracting comments with PRAW。
根据链接的文档产量修改代码
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)
这将在提交内容上打印所有顶级注释。请注意,Comment
类具有其他属性,其中许多属性已记录在here中。例如,要打印用红色圈出的comment
的某些属性,请尝试:
print(comment.author)
print(comment.score)
print(comment.created_utc) # as a Unix timestamp
print(comment.body)
如链接的文档所建议,您可以使用.list()
方法获得提交中的所有评论:
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)