使用praw解析评论时遇到问题
我正在尝试扫描一个特定的子版块,看看某条评论在热门帖子中出现了多少次。
我还没有找到任何迹象表明程序实际上在读取消息,因为它根本不打印出消息的内容。
注意: sr = 子版块 phrase = 正在寻找的短语我对praw和python还很陌生(刚刚学了一个小时),但我在c语言方面有一些经验。
任何帮助都将不胜感激。
submissions = r.get_subreddit(sr).get_top(limit=1)
for submission in submissions:
comments = praw.helpers.flatten_tree(submission.replace_more_comments(limit=None, threshold=0))
for comment in comments:
print(comment.body.lower())
if comment.id not in already_done:
if phrase in comment.body.lower():
phrase_counter = phrase_counter + 1
1 个回答
1
Submission.replace_more_comments
这个方法会返回一个列表,里面包含那些MoreComment
对象,这些对象是没有被替换掉的。所以,如果你调用这个方法时设置了limit=None
和threshold=0
,那么它会返回一个空列表。你可以查看replace_more_comments
的文档说明。下面是一个完整的例子,展示了如何使用replace_more_comments
和flatten_tree
这两个方法。想了解更多信息,可以查看我们文档中的评论解析页面。
import praw
r = praw.Reddit(UNIQUE_AND_DESCRIPTIVE_USERAGENT_CONTAINING_YOUR_REDDIT_USERNAME)
subreddit = r.get_subreddit('python')
submissions = subreddit.get_top(limit=1)
for submission in submissions:
submission.replace_more_comments(limit=None, threshold=0)
flat_comments = praw.helpers.flatten_tree(submission.comments)
for comment in flat_comments:
print(comment.body)