node.js - how to save file with request in json nodejs? -


i'm trying save result json file when see goes in half, wrong in code not understand part, help.

var request = require("request"); var cheerio = require("cheerio"); var fs = require('fs'); var urls = ["http://www.fordencuotas.com.ar"]  var req = function(url){     request({         uri: url,     }, function(error, response, body) {         var $ = cheerio.load(body);         $("a").each(function() {         var link = $(this);         var itri = {iti: new array(link.attr("href"))}         var data = json.stringify(itri);         fs.writefile("file.json", data, function(err){             if(err){console.log(err);} else {console.log("archivo guardado..");}         });         });     }); }  (var = 0; < urls.length; i++){     req(urls[i]); }  console.log("cargando..."); 

this output

[opmeitle@localhost crawler1]$ node crawmod.js cargando... archivo guardado.. archivo guardado.. archivo guardado.. archivo guardado.. archivo guardado.. ... archivo guardado.. [opmeitle@localhost crawler1]$ cat file.json {"iti":["productos/autos/nuevo-focus.html"]}us.html"]} [opmeitle@localhost crawler1]$  

there's couple of issues in code.

first, trying overwrite same file (file.json) each a element. i'm not sure if that's intention, seems rather pointless.

secondly, fs.writefile asynchronous. means node doesn't wait until file written before returns loop. in other words, each a element open same file, while might have been opened earlier iteration of loop. , each iteration writing same file, you're going end unexpected results.

you can either use fs.writefilesync synchronously write file, make node wait until data has been written file before continuing, or gather data want saved file in variable, , — after $("a").each(...) loop — write variable file once.

that last solution this:

var data = []; $("a").each(function() {    var link = $(this);   var itri = {iti: new array(link.attr("href"))}   data.push( itri ); }); fs.writefile("file.json", json.stringify(data), function(err){   if(err){console.log(err);} else {console.log("archivo guardado..");} }); 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -