我正在尝试使用 API 获取 subreddit 中的所有链接,但它只返回一个 url。这是我的代码:
var request = require('request');
webpage = 'http://www.reddit.com/r/AmazonUnder5/top.json?limit=100';
//login
request.post('http://www.reddit.com/api/login',{form:{api_type:'json', passwd:'password', rem:true, user:'username'}});
//get urls
request({uri : webpage, json:true, headers:{useragent: 'mybot v. 0.0.1'}}, function(error, response, body) {
if(!error && response.statusCode == 200) {
for(var key in body.data.children) {
var url = body.data.children[key].data.url;
console.log(url);
}
}
});
当我在浏览器中访问 json 链接时,它会返回所有 100 个帖子。
最佳答案
那是因为顶部只有1个
http://www.reddit.com/r/AmazonUnder5/top
你可以改用hot
http://www.reddit.com/r/AmazonUnder5/hot.json
此外,您无需登录即可执行公共(public)获取请求
编辑:你得到的结果太少是因为你没有正确登录
登录时,使用
"op" => "login"
参数化并测试返回的 cookie 和数据。
我还推荐使用 ssl 登录 url,因为它对我有用
https://ssl.reddit.com/api/login/
关于node.js - Reddit API 只返回一个帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17900952/