narek
narek

Reputation: 1036

Write results into a file using CasperJS

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

Upvotes: 43

Views: 35387

Answers (4)

iChux
iChux

Reputation: 2376

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

Upvotes: 10

q0rban
q0rban

Reputation: 987

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.

Upvotes: 6

Alejandro Silva
Alejandro Silva

Reputation: 9108

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');
});

Upvotes: 5

Henry
Henry

Reputation: 1424

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";

Upvotes: 70

Related Questions