2

I'm trying to play around with the API of Reddit, and I understand most of it, but I can't seem to figure out how to access the next page of results (since each page is 25 entries).

Here is the code I'm using:

import requests
import json

r = requests.get(r'https://www.reddit.com/r/Petscop/top.json?sort=top&show=all&t=all')

listing = r.json()

after = listing['data']['after']

data = listing['data']['children']

for entry in data:
    post = entry['data']
    print post['score']

query = 'https://www.reddit.com/r/Petscop/top.json?after='+after
r = requests.get(query)

listing = r.json()

data = listing['data']['children']

for entry in data:
    post = entry['data']
    print post['score']

So I extract the after ID as after, and pass it into the next request. However, after the first 25 entries (the first page) the code returns just an empty list ([]). I tried changing the second query as:

r = requests.get(r'https://www.reddit.com/r/Petscop/top.json?after='+after)

And the result is the same. I also tried replacing "after" with "before", but the result was again the same.

Is there a better way to get the next page of results?

Also, what the heck is the r in the get argument? I copied it from an example, but I have no idea what it actually means. I ask because I don't know if it is necessary to access the next page, and if it is necessary, I don't know how to modify the query dynamically by adding after to it.

Nathan
  • 194
  • 1
  • 17
  • 1
    1) You will be much happier if you use [PRAW](https://praw.readthedocs.io/en/latest/), as it handles these sorts of details automatically. 2) That r is defining [a raw string](https://stackoverflow.com/q/2081640/120999). – Xiong Chiamiov Apr 12 '18 at 20:27

1 Answers1

2

Try:

query = 'https://www.reddit.com/r/Petscop/top.json?sort=top&show=all&t=all&after='+after

or better:

query = 'https://www.reddit.com/r/Petscop/top.json?sort=top&show=all&t=all&after={}'.format(after)

As for r in strings you can omit it.

Evgeny
  • 4,173
  • 2
  • 19
  • 39