Save to file - file not created

17 views
Skip to first unread message

Luis P. Mendes

unread,
Nov 5, 2015, 6:44:28 AM11/5/15
to CasperJS
Hi,

This script doesn't create a csv file with some headers.
What I want is to create the file with heading before starting the scrape and then add data to it in append mode as the scraping goes.

What's wrong? How to create the file?

Luis


// open csv file in write mode and save the title to it.
function initializeCsvFile() {
   
var str = '';

   
var fields = ["header1", "header2"];

   
// build title line
   
var line = '';
   
for (var f = 0; f < fields.length; ++f) {
       
if (line != '') line += '|'
        line
+= fields[f];
   
}

    str
+= line + '\n';

   
var fs = require('fs');
    fs
.write(csv_file, str, 'w');
    casper
.log("csv_file: " + csv_file);
    casper
.log("str: " + str);
    fs
.close();
}

casper
.start(function() {
    initializeCsvFile
();
});

casper
.run(function() {
   
this.exit();
});



Luis P. Mendes

unread,
Nov 9, 2015, 7:51:11 AM11/9/15
to CasperJS
Any help on this?
Reply all
Reply to author
Forward
0 new messages