3

I'm having a problem downloading files from urls I am creating when scrapping a website. Currently I discovering the month and year of a file then replacing the values in a url and downloading trying to download from that location. I understand that you can't use the download function from inside the evaluation scope.

this.evaluate(function collectAllData (MONTHS) {
    for (...) {
        // Create url from page information ...
        casper.emit('test.download', url, fileName); 
    }
}, MONTHS);

casper.on('remote.download', function processRemoteDownload(url, fileName) {
    this.download(url, fileName);
});

Is there anyway to emit a custom event from within evaluate? I don't want to navigate away from the current page I am on or have to go back and forth from the evaluate scope. I know I could return a list of urls and process them after the fact but was curious if this was possible. Thanks for any help.

4

2 回答 2

1

evaluate回调中使用:

console.log("casper-event:add:[1234]");

然后可以这样做(未经测试):

casper.on('remote.message', function(msg) {
   if(msg.indexOf("casper-event:" == 0))
   {
       var event = msg.replace(/^casper-event:/, '').replace(/:.*$/, '');
       var result = JSON.parse(msg.replace(/^casper-event:.*?:/, ''));
       this.emit(event, result);
   }
})

casper.on('add'........
于 2014-08-06T14:53:00.870 回答
0

这是我所做的DOMContentLoaded

casper.then(function getDOMLoaded(){
    this.evaluate(function(){
        window.__DOMLoaded = false;
        document.addEventListener("DOMContentLoaded", function(){
            window.__DOMLoaded = true;
        })
    })
});
casper.waitFor(function check() {
    return this.getGlobal('__DOMLoaded');
}, function then() {    // step to execute when check() is ok
    casper.echo("DOMContentReady in " + (Date.now()-start) + " ms", "INFO_BAR");
}, function timeout() { // step to execute if check has failed
    casper.echo("DOMContentReady took longer than 10 seconds!")
}, 10000);

它设置了一个全局变量,该变量在 WebPage 中通过evaluate. 然后我运行一个waitForwhich 尝试(持续 10 秒)来检查是否window.__DOMLoaded为真(但是,在 casper 内但不评估,这是通过this.getGlobal()

于 2014-05-30T10:11:14.017 回答