用 praw 发帖时自动添加评论到 reddit

automating adding comment to reddit when posting with praw

我正在创建一个 reddit 机器人。当我创建一个包含图像和标题的 post 时。我想在创建的 post 下自动添加一条评论,其中包含一段文字 ("hello world").

我用的是python和大虾

我看过文档

https://praw.readthedocs.io/en/v3.6.0/pages/code_overview.html?highlight=comment#praw.objects.Submission.add_comment

但我无法全神贯注并使其发挥作用。

这是我用来 post 带有标题和图像的 post 的代码。

reddit.subreddit(subreddit).submit_image(title, image_path)

我怎样才能向创建的 post 添加评论?

您正在查看旧版本 PRAW (3.6.0) 的文档,该版本至少自 2016 年以来就没有更新过。可以找到最新的文档 here. Based on your usage of submit_image, I guess you are using at least PRAW 6.1.0, since the method was added in that version.

submit, submit_image, and submit_video all return the newly created Submission. All you have to do then is replySubmission 的方法如下:

my_post = reddit.subreddit(subreddit).submit_image(title, image_path)
my_post.reply("This is a comment.")

如果您不需要对 post 执行任何操作(因此不需要将其存储在变量中),您甚至可以在一行中执行此操作:

reddit.subreddit(subreddit).submit_image(title, image_path).reply("This is a comment.")