首页 > 解决方案 > 在 Reddit 上获取图片帖子的 URL

问题描述

我正在开发一个 reddit 机器人,其目的是在检测到包含“!repostfinder”的评论时查找转发。机器人能够检测到字符串,但我不知道如何获取被评论的图像。

这是我到目前为止的代码:

#subreddit to use
subreddit = reddit.subreddit('test')

#summoning the bot
keyphrase = '!repostfinder'

#find comments with keyphrase
for comment in subreddit.stream.comments():
    if keyphrase in comment.body:
        print('Found keyphrase')
        comment.reply('Keyphrase detected')
        print('Replied to comment')

标签: pythonredditpraw

解决方案


praw您应该阅读您正在使用的库的相关文档。

以下是以下文档praw.models.reddit.comment.Commenthttps ://praw.readthedocs.io/en/latest/code_overview/models/comment.html?highlight=comment

您可以使用 获取评论的提交comment.submission。然后由您决定如何处理数据。以下是以下文档praw.models.reddit.submission.Submissionhttps ://praw.readthedocs.io/en/latest/code_overview/models/submission.html?highlight=submission

例子:

# Fetch some comments
comments = []
for comment in subreddit.stream.comments():
    # Stop after fetching some comments
    if (len(comments) < 10):
        comments.append(comment)
    else:
        break
# Select specific comment
comment = comments[0]
# Get the comment's submission
submission = comment.submission

推荐阅读