3

我正在尝试在 dom 中寻找资源,以便我关注当前页面的每个链接。问题是第一次效果很好,但是当我开始点击链接时,我收到“cookies are not enabled”。我这样调用脚本: casperjs --cookies-file=/tmp/mycookies.txt script.js 这是脚本:

var casper = require('casper').create({
    logLevel: "debug",              // Only "info" level messages will be logged
    verbose:true,
    onError: function(self, m) {   // Any "error" level message will be written
        console.log('FATAL:' + m); // on the console output and PhantomJS will
        self.exit();               // terminate
    },
    pageSettings: {
        javascriptEnabled: true,
        loadImages:  false,        // The WebPage instance used by Casper will
        loadPlugins: false,         // use these settings
        userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'
    }
});
var fs = require('fs');
casper.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4');
phantom.cookiesEnabled = true;
var x = require('casper').selectXPath;

function getLinks() {
    var links = document.querySelectorAll('a');
    return Array.prototype.map.call(links, function(e) {
        return e.getAttribute('href')
    });
}

function followLinks(links) {
    casper.each(links, function(self, link) {
          self.thenOpen(link, function() {
              fs.write("pages/" + link+ ".html", this.getHTML(), 'w'); //for debugging
              if (!lookFor(x('//input[@name="Export"]'))) {
                    var links = getLinks();
                    followLinks(links);
              }
         });
  });
}
casper.start('http://www.url.com', function() {
   this.fill('form#formAuth', { 
        login: 'user@url.com', 
        password:  'apass'
    }, true);
});

casper.then(function() {
    links = links.concat(this.evaluate(getLinks));
    followLinks(links);
});

casper.run(function() {
    this.exit();
});
4

1 回答 1

1

问题是其中一个链接是注销链接:D。这就是为什么我突然收到错误

于 2013-03-30T17:35:25.467 回答