python - PRAW 不理解之后的关键字参数

标签 python typeerror reddit traceback praw

我正在尝试用 python 编写一个程序,它从 reddit 获取一篇带有 100 多条评论的帖子,其中一条评论需要超过 100 个单词。该评论与我想要的标题相结合。 这是我的代码,除了登录数据:

subreddit_to_scrape = "askreddit"
used_posts_file = "C:\\Users\\Yoga Duet 7\\OneDrive - BRG Kepler\\alt\\Desktop\\RedditBot\\used_posts.txt"


import praw

reddit = praw.Reddit(
    client_id=REDDIT_CLIENT_ID,
    client_secret=REDDIT_SECRET_KEY,
    username=REDDIT_USERNAME,
    password=REDDIT_PASSWORD,
    user_agent=REDDIT_USER_AGENT)


def get_new_posts(subreddit_to_scrape, after=None, limit=1):
    subreddit = reddit.subreddit(subreddit_to_scrape)
    if after is not None:
        new_post = subreddit.new(limit=limit, after=after)
    else:
        new_post = subreddit.new(limit=limit)
    return new_post

def add_new_post(subreddit_to_scrape, list=None):
    new_post = True
    after = ""
    while new_post:
        if after != "":
            posts = get_new_posts(subreddit_to_scrape, after)
        else:
            posts = get_new_posts(subreddit_to_scrape)
        for post in posts:
            post.comments.replace_more()
            print(len(post.comments.list()))
            if len(post.comments.list()) >= 100:
                for comment in post.comments.list():
                    if len(comment.body) >= 100:
                        new_post = False
                        return post.title, comment.body
                    else:
                        after = post.id
            else:
                after = post.id



print(add_new_post(subreddit_to_scrape))

这是错误:

python main.py
0
Traceback (most recent call last):
  File "C:\Users\Yoga Duet 7\OneDrive - BRG Kepler\alt\Desktop\RedditBot\main.py", line 52, in <module>
    print(add_new_post(subreddit_to_scrape))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Yoga Duet 7\OneDrive - BRG Kepler\alt\Desktop\RedditBot\main.py", line 34, in add_new_post
    posts = get_new_posts(subreddit_to_scrape, after)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Yoga Duet 7\OneDrive - BRG Kepler\alt\Desktop\RedditBot\main.py", line 24, in get_new_posts
    new_post = subreddit.new(limit=limit, after=after)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\praw\models\listing\mixins\base.py", line 115, in new
    return ListingGenerator(self._reddit, url, **generator_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: ListingGenerator.__init__() got an unexpected keyword argument 'after'

所以我不确定现在该怎么办。

我已经尝试用之前替换之后,同样的错误,但我认为之后是我需要的。

最佳答案

已解决。我需要将“after”参数作为参数传递,如下所示:

new_post = subreddit.new(limit=limit, params={'after': after})

关于python - PRAW 不理解之后的关键字参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75983258/

相关文章:

python - 拦截模块调用?

Ruby: erb throwing error "` result': can't convert String into Integer (TypeError)”

python - 合并字典时出现类型错误 : unsupported operand type(s) for |: 'dict' and 'dict'

python - urllib2 HTTP 错误 429

javascript - 类型错误 : Cannot read property 'state' of undefined

python - 来自数据库的加权边列表的图形

python - 在 Python Django 中渲染基本 html 模板内的子页面

python - 附加失败时如何有效地重建 pandas hdfstore 表

javascript - 用Java编写Conways Life of Life错误

python - 在praw中,我正在尝试打印评论正文,但是如果我遇到空评论怎么办?