Write results into a file using CasperJS

后端 未结 4 2008
一生所求
一生所求 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: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');
    });
    

提交回复
热议问题