Write results into a file using CasperJS

后端 未结 4 2004
一生所求
一生所求 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

    0 讨论(0)
  • 2020-12-13 06:42

    Here is a helper function that you can use to add this functionality to the casper object.

    /**
     * Save page markup to a file. Respect an existing savePageContent function, if
     * casper.js core introduces one.
     * 
     * @param String targetFile
     *   A target filename.
     * @return Casper
     */
    casper.savePageContent = casper.savePageContent || function(targetFile) {
      var fs = require('fs');
      var f  = require('utils').format;
    
      // Get the absolute path.
      targetFile = fs.absolute(targetFile);
      // Let other code modify the path.
      targetFile = this.filter('page.target_filename', targetFile) || targetFile;
      this.log(f("Saving page html to %s", targetFile), "debug");
      // Try saving the file.
      try {
        fs.write(targetFile, this.getPageContent(), 'w');
      } catch(err) {
        this.log(f("Failed to save page html to %s; please check permissions", targetFile), "error");
        this.log(err, "debug");
        return this;
      }
    
      this.log(f("Page html saved to %s", targetFile), "info");
      // Trigger the page.saved event.
      this.emit('page.saved', targetFile);
    
      return this;
    };
    

    It is helpful to note that 'fs' in this case is not the Node JS FileSystem object, but rather a PhantomJS module.

    0 讨论(0)
  • 2020-12-13 06:57

    A full 'then' function, that scrap data from a site, returns a json and store it to a file"myFile" should looks like this:

    casper.then(function paso2() {
    var jsonStr = this.evaluate(function(){
        var puntos = {};
        puntos.alafecha = document.querySelector('div.cont_item_productos_puntos > p.txt_negro').textContent;
        puntos.totales = document.querySelector('ul.lista_prod_puntos > li.ppuntos_1 > span.ppuntos_2').textContent;
        return JSON.stringify(puntos);
    });
    console.log("this is a response in json format: "+json);
    fs.write('myFile.json', jsonStr, 'w');
    });
    
    0 讨论(0)
  • 2020-12-13 06:58
    var fs = require('fs');
    fs.write(myfile, myData, 'w');
    

    for saving daily scrapes I do:

    var currentTime = new Date();
    var month = currentTime.getMonth() + 1;
    var day = currentTime.getDate();
    var year = currentTime.getFullYear();
    var myfile = "data-"+year + "-" + month + "-" + day+".html";
    
    0 讨论(0)
提交回复
热议问题