Get Reddit user comments using PRAW causing TypeError: 'SubListing' object is not callable error

Get Reddit user comments using PRAW causing TypeError: 'SubListing' object is not callable error

我正在尝试检索用户的最后 1000 条评论,因为 1000 条是 Reddit 的限制。

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

这是我 运行 的代码。

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) 并且它显示正确。

知道我做错了什么吗?

根据文档,comments is an attribute of the Redditor model in PRAW 4, not a function. Therefore, calling .comments(limit=None) is invalid syntax because .comments isn't a function. Instead, you must specify a listing sort order, like so, because SubListing objects (what user.comments is) inherit from BaseListingMixin:

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

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