Python reddit API: 高效解析 subreddit 中的所有评论
Python reddit API: efficiently parse all comments in a subreddit
我正在尝试编写聊天机器人代码并让它扫描添加到其中的所有评论。
目前我通过每 X 秒扫描一次最后 Y 条评论来做到这一点:
handle = praw.Reddit(username=config.username,
password=config.password,
client_id=config.client_id,
client_secret=config.client_secret,
user_agent="cristiano corrector v0.1a")
while True:
last_comments = handle.subreddit(subreddit).comments(limit=Y)
for comment in last_comments:
#process comments
time.sleep(X)
我很不满意,因为可能会有很多重叠(这可以通过跟踪评论 ID 来解决)并且一些评论被扫描了两次而其他评论被忽略了。 API 有更好的方法吗?
我在 PRAW API 中找到了利用 stream
的解决方案。 https://praw.readthedocs.io/en/latest/tutorials/reply_bot.html
中的详细信息
在我的代码中:
handle = praw.Reddit(username=config.username,
password=config.password,
client_id=config.client_id,
client_secret=config.client_secret,
user_agent="cristiano corrector v0.1a")
for comment in handle.subreddit(subreddit).stream.comments():
#process comments
这应该可以节省一些 CPU 和网络负载。
我正在尝试编写聊天机器人代码并让它扫描添加到其中的所有评论。
目前我通过每 X 秒扫描一次最后 Y 条评论来做到这一点:
handle = praw.Reddit(username=config.username,
password=config.password,
client_id=config.client_id,
client_secret=config.client_secret,
user_agent="cristiano corrector v0.1a")
while True:
last_comments = handle.subreddit(subreddit).comments(limit=Y)
for comment in last_comments:
#process comments
time.sleep(X)
我很不满意,因为可能会有很多重叠(这可以通过跟踪评论 ID 来解决)并且一些评论被扫描了两次而其他评论被忽略了。 API 有更好的方法吗?
我在 PRAW API 中找到了利用 stream
的解决方案。 https://praw.readthedocs.io/en/latest/tutorials/reply_bot.html
在我的代码中:
handle = praw.Reddit(username=config.username,
password=config.password,
client_id=config.client_id,
client_secret=config.client_secret,
user_agent="cristiano corrector v0.1a")
for comment in handle.subreddit(subreddit).stream.comments():
#process comments
这应该可以节省一些 CPU 和网络负载。