Warm tip: This article is reproduced from stackoverflow.com, please click
praw python reddit

Which praw commands need a network request?

发布于 2020-05-26 18:53:22

Am I right that each line of this code makes only one request?

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

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

Questioner
F. Vosnim
Viewed
10
jarhill0 2020-03-11 10:34

This snippet makes two requests. First, when you access sm.comments on line 3, that loads the comments of the submission from the Reddit API. Then, on that same line, calling replace_more(1) will replace exactly one MoreComments object, which makes an additional request.

To figure out how many network requests are being made by some PRAW code, take a look at the section from the documentation on logging. Add this snippet from that page at the top of your script:

import logging

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

Then, when you run your script, you'll see debugging output that describes each request made. For your snippet, my output looked something like this:

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)

Each line that starts with "Fetching" is another network request, and the subsequent lines describe the request further.


Frame challenge

You say

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

While I can't know exactly what you mean by "use api more effectively," if your concern is that you'll go over the rate-limit, you don't have to worry about that at all. One of the key features of PRAW is that it handles the rate limit for you, ensuring that you make requests as frequently as allowed without violating the rate-limit.