0

情况:我想创建一个多线程脚本,在其中使用 CSV 加载 IP 列表 + 帐户信息。我加载数据并调用一个函数,在该函数中我打开电子并结合 Vo 运行我的噩梦脚本。在脚本中,我访问一个站点,遍历链接列表并检查是否有人住在澳大利亚。当我遇到错误(例如超时)时,浏览器将停止工作。错误示例 ->

{ message: 'navigation error',
  code: -7,
  details: 'Navigation timed out after 30000 ms',
  url: 'https://facebook.com/login' }

这是我的代码

var fs = require('fs');
var csv = require('fast-csv');
var vo = require('vo');
var Nightmare = require('nightmare');

    var count = 0;
    var urls = fs.readFileSync('uniqueIds.csv').toString().split("\n");

    var arrayUrls = Object.keys(urls).map(function (key) {return urls[key]});

    var bloqNumber = 0;
    function *run(proxy, user, pass, urlsID) {
        var nightmare = new Nightmare({ 
            webPreferences: { partition: 'your-custom-partition'},
            switches:{
            'proxy-server': proxy,
            'ignore-certificate-errors': true
        }, show: true });

        yield nightmare
        .goto('https://facebook.com/login')
        .wait(".inputtext._55r1.inputtext._1kbt.inputtext._1kbt")
        .type('input[name="email"]', user)
        .type('input[name="pass"]', pass)
        .click('button[name=login]')
        .wait(29000);

        var range = urlsID * 2000;
        var rangeStart = range - 2000;
        var urlsarray = arrayUrls.slice(rangeStart, range);
        for (var i = 0; i < urlsarray.length; i++) {
                count++;
                console.log(count + " -> " + proxy);
                if (count > 150){
                    yield nightmare.end();
                }
                yield nightmare
                .goto("https://www.facebook.com/profile.php?id=" + urlsarray[i] + "&sk=about&section=living&pnref=about")
                .wait(1000);
                var seqCheck = yield nightmare.exists(".captcha_interstitial");
                var bloqCheck = yield nightmare.exists(".mvl.ptm.uiInterstitial.uiInterstitialLarge.uiBoxWhite");
                if (seqCheck == true) {
                  console.log("Seqcheck");
                    yield nightmare.wait(29000);
                }
                if (bloqCheck == true) {
                  console.log("Blocked for a week" + user + proxy);
                  bloqNumber++;
                  console.log(bloqNumber);
                if (bloqNumber > 6) {
                  yield nightmare.end();
                }
                  continue;
                }

                var location = yield nightmare.exists("._3pw9._2pi4._2ge8");
                bloqNumber = 0;
                console.log(location);
                if (location == true) {
                  var getLocation = yield nightmare.evaluate(function() {
                                  var jsonObject = new Array();
                                  var links = document.getElementsByClassName('_3pw9 _2pi4 _2ge8');
                                  var numProfiles = links.length;
                                    for(var i = 0; i< numProfiles; i++){
                                    var elem;
                                    try {
                                    elem = links[0].querySelector("._50f5._50f7 a").text;
                                      } catch (err) {
                                        var arrr = new Array('Hello', 'world');
                                        return arrr;
                                      }
                                    jsonObject.push(elem);
                                  }
                                  return jsonObject;
                                });
                 var locationString = getLocation.join(" + ");
                 console.log(locationString + " -> " + urlsarray[i]);
                   if (locationString.indexOf("Australia") !== -1 ||
                      locationString.indexOf("Queensland") !== -1 || 
                      locationString.indexOf("New South Wales") !== -1 || 
                      locationString.indexOf("Victoria") !== -1 ||
                      locationString.indexOf("Northern Territory") !== -1 || 
                      locationString.indexOf("South Australia") !== -1||
                      locationString.indexOf("Tasmania") !== -1 ||
                      locationString.indexOf("Sydney") !== -1 ||
                      locationString.indexOf("Adelaide") !== -1 ||
                      locationString.indexOf("Cairns") !== -1 ||
                      locationString.indexOf("Perth") !== -1 ||
                      locationString.indexOf("Melbourne") !== -1 ||
                      locationString.indexOf("Brisbane") !== -1 ||
                      locationString.indexOf("Bundaberg") !== -1 ||
                      locationString.indexOf("Canberra") !== -1 ||
                      locationString.indexOf("Newcastle") !== -1 ||
                      locationString.indexOf("Western Australia") !== -1 ) {
                    console.log("Im in australia");

                 var stringToPrint = urlsarray[i] + ", " + locationString + "\n";
                    fs.appendFile('pages.csv', stringToPrint.replace(/(\r\n|\n|\r)/gm,"") + "\n", function (err) {
                        console.log("a new entry");
                      });
                    }
                    } else {
                      console.log("It was false");
                    }

                }
                yield nightmare.end();
        }  

fs.createReadStream('proxies.csv')
    .pipe(csv())
    .on('data', function (data) {
        var proxy = data[0];
        var user = data[1];
        var pass = data[2];
        var urlsID = data[3];
        console.log(urlsID);
        console.log(user);
        console.log(pass);
        vo(run(proxy, user, pass, urlsID)).then(out => console.log('out', out)).catch(error => console.log(error));
    }).on('end', function (data) {
    console.log('CSV reading finished.')
});

期望的结果:我希望每次遇到某种错误时我的线程正在关闭。

4

1 回答 1

0

解决了。只需附加 .catch ,如下例所示。

yield nightmare
.goto('https://facebook.com/login')
.wait(".inputtext._55r1.inputtext._1kbt.inputtext._1kbt")
.type('input[name="email"]', user)
.type('input[name="pass"]', pass)
.click('button[name=login]')
.wait(29000).catch(function(err){
                  console.dir(err);
                  nightmare.end();
                });
于 2016-09-05T01:10:04.987 回答