python - 在 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')
解决方案
praw
您应该阅读您正在使用的库的相关文档。
以下是以下文档praw.models.reddit.comment.Comment
:https ://praw.readthedocs.io/en/latest/code_overview/models/comment.html?highlight=comment
您可以使用 获取评论的提交comment.submission
。然后由您决定如何处理数据。以下是以下文档praw.models.reddit.submission.Submission
:https ://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
推荐阅读
- python - 如何在 web2py 的编辑表单页面中重新填充 SQLFORM 字段
- java - 使用匿名扫描上传的 SonarQube 强制身份验证
- c++ - VS2017 是否允许在 C++ 项目的可视对话框编辑器中更改控件的选项卡顺序?
- java - 根据 JMX 数据确定 GC 运行时间
- amazon-web-services - AWS Lambda - 是否可以限制 IAM 角色创建具有特定前缀的函数?
- r - 如何在R中按条件分组?
- c# - 更改列表中的参考值
- angular - 在 Ng-Select 中更改颜色
- python - Django Signal.disconnect 引发“函数”对象没有属性“锁定”错误
- gnuradio - 连接到 git.gnuradio.org 时出现超时问题,如何解决?