PRAW不理解关键字参数之后

huangapple go评论81阅读模式
英文:

PRAW doesn't understand keyword argument after

问题

我正在尝试用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'

所以我不确定接下来该怎么做。

英文:

I am trying to write a program in python, which gets me a post from reddit with more than 100 comments, from which one needs to have over 100 Words. That comment in combination with the title i would like to have.
This is my Code, except login Data:

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


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 = &quot;&quot;
	while new_post:
		if after != &quot;&quot;:
			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()) &gt;= 100:
				for comment in post.comments.list():
					if len(comment.body) &gt;= 100:
						new_post = False
						return post.title, comment.body
					else:
						after = post.id
			else:
				after = post.id



print(add_new_post(subreddit_to_scrape))

And this is the error:

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

So I am not sure on what to do now.

I already tried replacing the after with before, same error, but i think after is what i need.

答案1

得分: 1

解决了。我需要将 'after' 参数传递为这样的参数:

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

Solved. I needet to pass the 'after' argument as a parameter like this:

new_post = subreddit.new(limit=limit, params={&#39;after&#39;: after})

huangapple
  • 本文由 发表于 2023年4月11日 15:07:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定