英文:
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 = "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))
And this is the error:
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'
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={'after': after})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论