使用请求抓取所有时间的 subreddit 热门帖子返回错误的结果

Scraping subreddit top posts of all time using requests is returning the wrong result

我想为他们 一直 的热门帖子抓取一个 subreddit。我知道有一个 PRAW 模块可能工作得更好,但我更愿意暂时只使用 requests 进行抓取。

import requests

url = "https://www.reddit.com/r/shittysuperpowers/top/?t=all.html"
headers = {"User-agent": "bot_0.1"}
res = requests.get(url, headers=headers)

res.status_code返回200,抓取成功。但仔细检查 res.text 后发现,抓取的数据 html 并非来自所需页面。事实上,被抓取的是今天 的热门帖子,而不是所有时间,或者来自这个 url https://www.reddit.com/r/shittysuperpowers/top/?t=day.html。有什么理由让我一直无法抓取最热门的帖子吗?我也用其他 subreddits 尝试过这个,他们都 运行 遇到了同样的问题。

在查询前使用 .json 修饰符以获取 json 格式的数据。

import requests
url = 'https://www.reddit.com/r/shittysuperpowers/top/.json?sort=top&t=all'
resp = requests.get(url, headers = {'User-agent': 'bot_0.1'})
if resp.ok:
    data = resp.json()

.json修饰符也适用于浏览器。