在Reddit上获取图片帖子的网址

时间:2019-03-03 16:59:56

标签: python reddit praw

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

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

MATCH path = (A)-[*]->(B)
UNWIND nodes(path) AS n
UNWIND relationships(path) AS r
WITH 
  collect(DISTINCT path) as paths,
  collect(DISTINCT n) AS nodes, 
  collect(DISTINCT r) AS rels
RETURN
  [p IN paths | {
    nodes: [n IN nodes(p) | id(n)],
    rels: [r IN relationships(p) | id(r)]
  }] as paths,
  reduce(acc={}, n IN nodes | apoc.map.setKey(acc, toString(id(n)), n)) as nodes,
  reduce(acc={}, r IN rels | apoc.map.setKey(acc, toString(id(r)), r)) as rels

2 个答案:

答案 0 :(得分:1)

您应该阅读所使用的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

答案 1 :(得分:0)

每个注释都有一个parent_id值。如果其最上面的注释,则父母ID将以t3_开头(它是帖子,您需要检查),如果父ID是另一个注释,则其父母身份将以t1_开头,这意味着它是一个回复另一条评论,对您而言这将是无用的信息。

所以现在你去做

postData = reddit(id=subbmission.parrent_id[3:]) # This is how u get the post object
print(postData.url) # The URL of the image