在 Reddit 中访问列表结果的下一页 API
Access the next page of list results in Reddit API
我正在尝试使用 Reddit 的 API,我了解其中的大部分内容,但我似乎无法弄清楚如何访问下一页结果(因为每一页是 25 个条目)。
这是我使用的代码:
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']
所以我将after ID提取为after
,并将其传递到下一个请求中。但是,在前 25 个条目(第一页)之后,代码 returns 只是一个空列表 ([]
)。我尝试将第二个查询更改为:
r = requests.get(r'https://www.reddit.com/r/Petscop/top.json?after='+after)
结果是一样的。我也尝试用 "before" 替换 "after",但结果还是一样。
是否有更好的方法获取下一页结果?
此外,get 参数中的 r
到底是什么?我从一个例子中复制了它,但我不知道它的实际含义。我问是因为我不知道是否有必要访问下一页,如果有必要,我不知道如何通过添加 after
来动态修改查询。
尝试:
query = 'https://www.reddit.com/r/Petscop/top.json?sort=top&show=all&t=all&after='+after
或更好:
query = 'https://www.reddit.com/r/Petscop/top.json?sort=top&show=all&t=all&after={}'.format(after)
至于字符串中的r
可以省略
我正在尝试使用 Reddit 的 API,我了解其中的大部分内容,但我似乎无法弄清楚如何访问下一页结果(因为每一页是 25 个条目)。
这是我使用的代码:
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']
所以我将after ID提取为after
,并将其传递到下一个请求中。但是,在前 25 个条目(第一页)之后,代码 returns 只是一个空列表 ([]
)。我尝试将第二个查询更改为:
r = requests.get(r'https://www.reddit.com/r/Petscop/top.json?after='+after)
结果是一样的。我也尝试用 "before" 替换 "after",但结果还是一样。
是否有更好的方法获取下一页结果?
此外,get 参数中的 r
到底是什么?我从一个例子中复制了它,但我不知道它的实际含义。我问是因为我不知道是否有必要访问下一页,如果有必要,我不知道如何通过添加 after
来动态修改查询。
尝试:
query = 'https://www.reddit.com/r/Petscop/top.json?sort=top&show=all&t=all&after='+after
或更好:
query = 'https://www.reddit.com/r/Petscop/top.json?sort=top&show=all&t=all&after={}'.format(after)
至于字符串中的r
可以省略