Write results into a file using CasperJS

后端 未结 4 2009
一生所求
一生所求 2020-12-13 06:08

How do I create a file in the file system and place the contents of this.getPageContent() inside it?

4条回答
  •  一个人的身影
    2020-12-13 06:39

    You could also append to a text file using the method below

        var casper = require('casper').create();
        var fs = require('fs');
        var fname = new Date().getTime() + '.txt';
        var save = fs.pathJoin(fs.workingDirectory, 'nwaomachux', fname);
    
        casper.start('http://www.po3w.com/', function() {
            fs.write(save, this.getTitle() + '\n', 'w');
        });
    
        casper.thenOpen('http://phantomjs.org', function() {
            fs.write(save, this.getTitle(), 'a');
        });
    
        casper.run();
    
    • If the folder, nwaomachux, doesn't yet exist, it'll be automatically created for you.
    • If you saved the file as save.js, run it from PhantomJS with the following command on a Linux PC

    ./phantom casperjs/bin/bootstrap.js --casper-path=casperjs --cli save.js

提交回复
热议问题