Using Praw how do you get comment linked by a URL

喜夏-厌秋 提交于 2019-12-24 14:51:18

问题


I am trying to get the content linked to by a reddit URL, it could be a submission OR a comment, and I need to be able to get the corresponding object, does anyone know how to?


回答1:


If you are using PRAW (it seems you are from this answer) you can simply use the get_submission function for either case.

import praw
r = praw.Reddit('<USER AGENT>')
submission = r.get_submission('http://www.reddit.com/r/redditdev/comments/10msc8/how_to_calculate_more_comments_count_not_just/')
comment = r.get_submission('http://www.reddit.com/r/redditdev/comments/10msc8/how_to_calculate_more_comments_count_not_just/c6euu6b').comments[0]

To get the comment, we're using the permalink to the comment which returns the json data for the submission, along with the data for the comment and its children. However, in this case the comment tree will only have a single top-level comment thus comments[0] is the desired comment.




回答2:


In the end I did it a bit of a hacky way:

def getObjectFromLink(url):
    global r
    obj=praw.objects.Submission.get_info(r, url)
    if len(url.split('/'))==6:
        return obj
    else:
        return obj.comments[0]


来源:https://stackoverflow.com/questions/12463818/using-praw-how-do-you-get-comment-linked-by-a-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!