【问题标题】:praw: get submission flairpraw:获得提交天赋
【发布时间】:2017-10-24 03:09:47
【问题描述】:

我正在使用 PRAW 处理 reddit 提交,特别是已解决并将其“flair”属性设置为 SOLVED 的提交(如 here 所述)。

但是,当我检查天赋时,我得到“无”,即使我可以看到的提交已设置为已解决。

我有以下代码,它适用于已明确设置为 SOLVED 的提交。

solvedSubmission = reddit.submission(url='https://www.reddit.com/r/PhotoshopRequest/comments/6ctkpj/specific_can_someone_please_remove_kids_12467_i/')
pprint.pprint(vars(solvedSubmission))

这个输出:

{'_comments_by_id': {},  
'_fetched': False,
'_flair': None,
'_info_params': {}, 
'_mod': None, 
'_reddit': <praw.reddit.Reddit object at 0x10e3ae1d0>, 
'comment_limit': 2048, 
'comment_sort': 'best', 
'id': '6ctkpj'}

谁能提供任何关于我为什么在这篇文章和其他已解决的帖子上看到“无”的见解? reddit 还有其他方法可以跟踪我应该查看的已解决帖子吗?

谢谢!

【问题讨论】:

  • 您确定 reddit 实例有效吗?
  • 应该是。我使用我的 client_id、client_secret、user_agent、用户名和密码来调用它。但是,我认为我没有正确“获取”数据。当我尝试使用附加参数“fetch=True”获取 subreddit 时,我收到诸如“Reddit object has no attribute 'get_subreddit'”之类的错误,或者“__call__() got an unexpected keyword argument 'fetch'”praw.readthedocs.io/en/v3.6.0/pages/…跨度>

标签: reddit praw


【解决方案1】:

到现在为止(OP 后约 1 年)你可能已经解决了这个问题,但它出现在我所做的搜索中,既然我找到了答案,我会分享。

您从未看到任何相关信息的原因是因为PRAW uses lazy objects so that network requests to Reddit’s API are only issued when information is needed。您需要使其非惰性以检索所有可用数据。下面是一个最小的工作示例:

import praw
import pprint

reddit = praw.Reddit() # potentially needs configuring, see docs
solved_url = 'https://www.reddit.com/r/PhotoshopRequest/comments/6ctkpj/specific_can_someone_please_remove_kids_12467_i/'

post = reddit.submission(url=solved_url)
print(post.title) # this will fetch the lazy submission-object...
pprint.pprint(vars(solvedSubmission)) # ... allowing you to list all available fields

pprint-输出中,截至撰写此答案时(2018 年 3 月),您将发现以下字段:

...
'link_flair_text': 'SOLVED',
...

... 这是您要在代码中使用的内容,例如像这样:

is_solved = 'solved' == post.link_flair_text.strip().lower()

所以,为了结束这个,你需要让 PRAW 发出一个网络请求来获取提交对象,把它从一个惰性实例变成一个完整的实例;您可以通过打印、分配给变量或使用所需的字段名直接进入对象来完成此操作。

【讨论】:

    猜你喜欢
    • 2015-01-07
    • 1970-01-01
    • 2020-11-23
    • 2020-09-10
    • 2019-05-28
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 2014-01-16
    相关资源
    最近更新 更多