【问题标题】:How to access response Body after simulating a POST request in Node.js?在 Node.js 中模拟 POST 请求后如何访问响应正文?
【发布时间】:2015-09-05 17:10:19
【问题描述】:

我已经尝试了很长时间了。 我想从包含成人内容的 subreddit 中删除内容。 但是,问题在于,您必须先回答一个简单的问题,然后才能访问该页面,即您是否年满 18 岁。 我对源代码做了一些研究,发现解决方案是一个简单的 POST 请求。您需要在其中发送参数“over18=yes”。 但我的问题是,发布后我无法访问响应正文。

这是在 node.js 中使用 http 请求的代码。我什至用节点“请求”模块尝试过它,但也没有任何帮助。

希望能在这里找到可以帮助我的人。

var http = require("http");


var options = {
  host: 'www.reddit.com',
  port: 80,
  path: '/over18?dest=http%3A%2F%2Fwww.reddit.com%2Fr%2Fnsfw&over18=yes',
  method: 'POST'
};

var req = http.request(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

// write data to request body
req.write('data\n');
req.write('data\n');
req.end();

这是使用节点请求模块的代码

var request = require("request");
request.post({url:'http://www.reddit.com/over18?dest=http%3A%2F%2Fwww.reddit.com%2Fr%2Fnsfw', form: {over18:'yes'}}, function(err,httpResponse,body){
    console.log(body);
});

我尝试访问的 URL 是 http://www.reddit.com/r/nsfw

【问题讨论】:

    标签: node.js web-scraping http-post reddit


    【解决方案1】:

    我在 ahem 做一些研究时也遇到了这个问题。这是我的版本:

    var url = 'http://www.reddit.com/r/nsfw/';
    var request = require('request');
    request = request.defaults({jar: true });
    request.post({
        followAllRedirects: true,
        url: 'http://www.reddit.com/over18?dest=' + encodeURIComponent(url),
        form: {uh: '', over18: 'yes', }
    }, function(err, httpResponse, html) {
        …
    });
    

    Reddit's Node.js APIs也值得一试,我个人很喜欢Snoocore

    【讨论】:

    • 这比第一个答案更短更简单......并且工作得很好。太感谢了。对不起,我在这里只能“接受”一个答案。我希望你不要介意。感谢您调查它...大帮助。
    【解决方案2】:

    简而言之,当您点击 YES 按钮时,表单使用 POST 方法将 over18=yes 参数发送到 url http://www.reddit.com/over18?dest=http%3A%2F%2Fwww.reddit.com%2Fr%2Fnsfw。然后,服务器以 302 Redirection 标头、值为 over18=1 的 cookie 响应,最后使用 GET 请求重定向到 url http://www.reddit.com/r/nsfw。那么,服务器只是检查你是否有一个具有所需价值的 cookie。

    您需要做的就是使用 GET 方法使用 cookie 直接向最终 url 请求。

    var request = require("request");
    
    var target = "http://www.reddit.com/r/nsfw";
    
    var jar = request.jar();
    var cookie = request.cookie("over18=1");
    cookie.domain = "reddit.com";
    cookie.path = "/";
    
    jar.setCookie(cookie, target, function(error, cookie) {
        console.log(error);
        console.log(cookie);
    });
    
    request({
        uri: target,
        method: "GET",
        jar: jar
    }, function(error, response, body) {
        console.log(response.statusCode);
        console.log(body);
    });
    

    【讨论】:

    • 这是完美的。一个很大的帮助和非常有教育意义。不知道我们可以发送这样的cookie。太感谢了。像你这样的人让这个社区变得更好。干杯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多