使用auth使用节点js对网站进行报废

时间:2016-03-14 22:01:06

标签: node.js

      var getMatches = function(){
var sait = 'https://www.website.com'
request({ url: sait, jar:  true}, function(error, response, html){
if(!error){
var $ = cheerio.load(html)
var count =  ($('.TheMatch').length)
for(var i = 2; i< count + 2 ; i++){
    var live = ($('.TheMatch:nth-child('+i+') .LiveStatus').text())
    var nameMatch = ($('.TheMatch:nth-child('+i+') .MDxEventName').text())
    var time = ($('.TheMatch:nth-child('+i+') .DateTime').text().substring(8))
    var websiteCount = ($('.TheMatch:nth-child('+i+') .TotalBookies').text())
    if((websiteCount >= 25) && (live.length === 0) ){
        console.log('match ' + nameMatch)
        console.log('count Websites ' + websiteCount)
    }
}}})}

我想在这个网站上进行身份验证并保存cookie我该怎么做?并保存cookie所以我每次解析时都不需要登录?

1 个答案:

答案 0 :(得分:0)

嗯,我做了类似的任务。但问题是它始终是特定于站点的。无论如何,这样做的方法是使用请求库向网站的auth端点发出post请求。然后响应对象将包含适当的cookie,如sessionID或其他东西。然后保存此cookie并从头开始执行新的请求页面。以下是有关请求模块中的Cookie的文档:https://github.com/request/request#requestcookie。这对我很有用。

如何获得:

request.post({ url: "website/signin",
                    form: {
                        login: 'login',
                        password: "password",
                        remember: 1
                    }
                }, function(err, httpResponse, body) {
                    var cooka = false;
                    var cookies = httpResponse.headers['set-cookie'];
                    if (typeof cookies != "undefined") cooka = cookies[0];
                    if (typeof cooka != "undefined" && cooka != false) {
                        self.sessionId = cooka.substring(10, cooka.indexOf(";"));
                    } else {
                        return self.emit("error", "Can't get session id");
                    }
                });

如何设置:

var options = { uri: "desired url" };
            var j = request.jar();
            if (worker.source == "coquette") {
                var cookie = request.cookie('PHPSESSID=' + worker.sessionId);
                j.setCookie(cookie, url);
                options.jar = j;
            }
            request.get(options, function(error, response, body) {
                if (error || response.statusCode != 200) {

                } else {
               // work with body body
              }
          });