【问题标题】:Get Reddit user comments using PRAW causing TypeError: 'SubListing' object is not callable error使用 PRAW 获取 Reddit 用户评论导致 TypeError: 'SubListing' object is not callable 错误
【发布时间】:2016-12-07 00:43:31
【问题描述】:

我正在尝试从用户那里检索最后 1000 个 cmets,因为 1000 是 Reddit 限制。

我遵循代码示例here,并修改了一些更新API 的调用。比如user.get_cmets 现在好像只是user.cmets。

这是我运行的代码。

import praw

my_user_agent = 'USERAGENT'
my_client_id = 'CLIENTID'
my_client_secret = 'SECRET'

r = praw.Reddit(user_agent=my_user_agent,
                     client_id=my_client_id,
                     client_secret=my_client_secret)

user = r.redditor('REDDITUSERNAME')

for comment in user.comments(limit=None):
    print comment.body 

不过,我每次在最后一行都会出错。

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'SubListing' object is not callable

我已经连接到 API 并且有一个活动连接,因为我可以执行 print(user.comment_karma) 并且它显示正确。

任何想法我做错了什么?

【问题讨论】:

  • 你不应该对错误日志使用代码格式,meta question about that
  • @Stargateur 谢谢你的收获。根据元帖子编辑了问题。
  • 您使用的是 PRAW 3 还是 4?我相信自该示例以来 API 可能发生了一些变化。
  • 只是在黑暗中刺伤,但为什么你只有 user.cmets 而不是 user.get_cmets?
  • @Aurora0001 PRAW 4.

标签: python praw


【解决方案1】:

根据文档,comments 是 PRAW 4 中Redditor 模型的属性,而不是函数。因此,调用.comments(limit=None) 是无效的语法,因为.comments 不是函数。相反,您必须指定一个列表排序顺序,就像这样,因为SubListing 对象(user.comments 是什么)继承自BaseListingMixin

for comment in user.comments.new():
    print(comment.body)

诚然,PRAW 4 的文档非常不清楚,您可能会通过直接搜索代码找到最好的文档。

【讨论】:

  • 这很奇怪,属性definitely existsuser = r.redditor('REDDITUSERNAME') 有任何帮助吗? (而不是get_redditor
  • 抱歉忘记在代码中更改了。它现在只是 r.redditor 而不是 r.get_redditor,这就是我正在使用的我只是在修复该错误之前复制并粘贴代码块。我现在已经编辑了问题中的代码。
  • 感谢您解决了这个问题,尽管默认情况下它没有显示完整的 1000 条评论历史记录 - 这就是 (limit=none) 部分所做的。如果你说它是无效的语法,任何想法如何添加它。
  • @TheBeginningEnd 应该.new()函数有效(即user.comments.new(limit=None)应该有效)但对.comments(limit=None)无效。
  • @Aurora0001 我在文档中添加了一些示例。我希望你觉得它们有用:praw.readthedocs.io/en/latest/code_overview/other/…
猜你喜欢
  • 2016-03-25
  • 2022-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-31
  • 2021-10-22
  • 1970-01-01
  • 2019-10-08
相关资源
最近更新 更多