2013-07-29 8 views
7

Sto cercando di fare in modo che CasperJS possa aprire ogni collegamento in uno array di collegamenti. L'ho fatto in modo che dopo aver aperto un link, verrà visualizzato il titolo di quella pagina. Eppure quando lo eseguo, non viene visualizzato nulla.CasperJS - Come aprire tutti i collegamenti in una serie di collegamenti

Posso usare un for loop per visualizzare i collegamenti e funziona perfettamente.

Questo è il codice per quello che ho appena spiegato:

var x; 

casper.start(URL, function() { 

    x = links.split(" "); // now x is an array of links 

    for (var i = 0; j < x.length; i++) // for every link... 
    { 
     casper.thenOpen(partialURL + x[i], function() { // open that link 
      console.log(this.getTitle() + '\n'); // display the title of page 
     }); 
    } 

    this.exit(); 
}); 

casper.run(); 

Questo è un altro metodo che ho provato:

var x; 

casper.start(URL, function() { 
    x = links.split(" "); // now x is an array of links 
    this.exit(); 
}); 

for (var i = 0; j < x.length; i++) // for every link... 
{ 
    casper.thenOpen(partialURL + x[i], function() { // open that link 
     console.log(this.getTitle() + '\n'); // display the title of page 
    }); 
} 

casper.run(); 

Si dice che 'x' in indefinito. Si noti che ho impostato x per essere una variabile globale però. Qualsiasi modifica che potresti fare sarebbe grandiosa. Grazie.

+0

Ho appena realizzato l'unico problema che ho avuto è stato questa linea: 'for (var i = 0; j

+2

Inoltre, 'this.exit();' sembra rendere nulla lo script (potrei portarlo via o semplicemente cambiarlo in 'casper.then (function() {this.exit();});'). Se cambio quelli, il codice nella domanda funziona. –

risposta

7
var x; var i = -1; 

casper.start(URL, function() { 
    x = links.split(" "); // now x is an array of links 
}); 

casper.then(function() { 
    this.each(x, function() { 
     i++; // change the link being opened (has to be here specifically) 
     this.thenOpen((partialURL + x[i]), function() { 
      this.echo(this.getTitle()); // display the title of page 
     }); 
    }); 
}); 

casper.run(); 
0

Prova qualcosa di simile.

var x; 

casper.start(URL, function() { 
    x = links.split(" "); // now x is an array of links 
}); 

casper.then(function() { 
    this.eachThen(x, function(response) { 
     this.thenOpen((partialURL + response.data), function() { 
      this.echo(this.getTitle()); // display the title of page 
     }); 
    }); 
}); 

casper.run(); 

x stato indefinito perché il ciclo veniva eseguito prima casper.start. Nel codice sopra riportato, il blocco eachThen() è nidificato all'interno di un blocco casper.then per ritardarne l'esecuzione.

+0

Non succede nulla. –

+1

Ho provato a modificare la tua risposta e credo che sia stata negata per qualche strana ragione. Potresti aggiungere 'var i;' a fianco di 'var x;', e potresti anche aggiungere 'i ++;' sotto 'console.log ...' –

+0

L'ho modificato per usare eachThen invece di ripetere.Dovrebbe funzionare ora – hexid

7
var i = 0; 
var nTimes = x.length; 

casper.repeat(nTimes, function() { 
    //... do your stuff 
    i++; 
}); 

ha lavorato per me.

+0

Funziona anche per me. –

1
casper.start(); 
casper.each(Object.keys(array), function(casper, array_elem) { 
    this.thenOpen(partialURL+array[attay_item], function() { 
     ... 
}; 

E come errore "non definito". Cerca di non usarlo troppo. Ho riscontrato spesso questo errore con CasperJS, quindi preferisco scrivere casper invece di questo.

+0

Se 'array' è un array reale, allora è meglio usare' array.forEach (function (item) {casper.thenOpen (partial + item)}) '. –

2

Nel mio caso, ho dovuto raschiare un sito che aveva un numero sconosciuto di pagine. Ogni pagina (tranne l'ultima) aveva un collegamento <a class="next-page" href="/page/N">Next page</a> (dove N è il numero di pagina). Non c'era modo per il raschietto di sapere quando era finito, tranne quando il link "Pagina successiva" non era più presente.

Ovviamente è necessario apportare modifiche in base al tipo di collegamento di impaginazione esistente sulla pagina.

Ecco cosa ho fatto. YMMV.

// imports 
var fs = require('fs'); 

// scraper state 
var state = {page: 1, data: []}; 

// casper 
var casper = require("casper").create(); 

// scraper function 
function scrape() { 
    this.echo('Scraping page ' + state.page + '...', 'INFO'); 

    state.data = state.data.concat(this.evaluate(function() { 
    // get some stuff from the page 
    return someData; 
    }); 

    var nextUrl = this.evaluate(function() { 
    var nextLink = document.querySelector("a.next-page"); 
    return nextLink && nextLink.href; 
    }); 

    if (nextUrl) { 
    state.page = state.page + 1; 
    casper.thenOpen(nextUrl, scrape); // <- recursion 
    } 
}); 

// run 
casper.run(function() { 
    fs.write('./data.json', JSON.stringify(state.data, null, '\t'), 'w'); 
    this.echo('Done!', 'INFO'); 
}); 

Spero che questo aiuti qualcuno. Se hai altre domande, sarò felice di provare ad aiutarti.

0

ho risolto lo stesso problema con questo codice:

casper.then(function() { 
    var i = -1; 
    this.eachThen(locations, function() { 
     i++; 
     //Do stuff here like for example: 
     this.thenOpen(YOUR_URL, function() { 
      this.waitForSelector("MYSELECTOR", 
      function() { 

      },     
      function() { 

      }) 
     }); 
    }) 
}); 
5
casper.start('about:blank'); 

var urls = ['http://google.fr', 'http://yahoo.fr', 'http://amazon.fr']; 

casper.each(urls, function(casper, url) { 
    casper.thenOpen(url, function() { 
     this.echo("I'm in your " + url + "."); 
    }); 
}); 
Problemi correlati