哪些 praw 命令需要网络请求?

Which praw commands need a network request?

这段代码的每一行只发出一个请求,我说得对吗?

sm = reddit.submission(url="...")
sm.comment_sort = 'top'
sm.comments.replace_more(1)
comments = sm.comments.list()

我知道 Reddit api 在每 600 秒的时间段内提供 600 个请求。我需要此信息才能更有效地使用 api。

此代码段提出了两个请求。首先,当您在第 3 行访问 sm.comments 时,会从 Reddit API 加载提交的评论。然后,在同一行上,调用 replace_more(1) 将恰好替换一个 MoreComments 对象,这会发出额外的请求。

要了解某些 PRAW 代码发出了多少网络请求,请查看 logging 文档中的部分。在脚本顶部添加该页面的此片段:

import logging

handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
logger = logging.getLogger('prawcore')
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)

然后,当您 运行 您的脚本时,您将看到描述每个请求的调试输出。对于您的代码片段,我的输出看起来像这样:

Fetching: GET https://oauth.reddit.com/comments/fgi5bd/
Data: None
Params: {'limit': 2048, 'sort': 'top', 'raw_json': 1}
Response: 200 (116926 bytes)
Fetching: POST https://oauth.reddit.com/api/morechildren/
Data: [('api_type', 'json'), ('children', 'fk5u680,fk5tgxt,<--snip-->,fk5u67w,fk5ug3f'), ('link_id', 't3_fgi5bd'), ('sort', 'top')]
Params: {'raw_json': 1}
Sleeping: 0.21 seconds prior to call
Response: 200 (32753 bytes)

以"Fetching"开头的每一行是另一个网络请求,后续行进一步描述该请求。


框架挑战

你说

I know that Reddit api offers 600 requests in each 600 second time period. I need this info to use api more effectively.

虽然我不能确切地知道您所说的 "use api more effectively," 是什么意思,但如果您担心会超过速率限制,则完全不必担心。 PRAW 的主要功能之一是它为您处理速率限制,确保您在不违反速率限制的情况下尽可能频繁地发出请求。