Node js console.log is not showing anything

2019-09-09 01:30发布

问题:

I'm trying to scrap a webpage using node js.I think I've written the code and was able to run it without any errors but the problem is the console doesn't print anything no matter what I do.It is not showing any errors. What's the reason?

Here is the content that I want to scrap: https://paste.ee/r/b3yrn

var fs         = require('fs');
var request    = require('request');
var cheerio    = require('cheerio');

var htmlString = fs.readFileSync('scrap.html').toString();
var $          = cheerio.load(htmlString);
var json = [];

$('body > table:nth-child(1) > tbody > tr:nth-child(3) > td > table > tbody > tr > td:nth-child(2) > table > tbody > tr > td > table > tbody > tr').each(function(i, element) {
    json.push({});
    json[i].range = $(this).text().trim();

});


console.log(json);

回答1:

To be sure that your console.log work correctly just try it like that :

console.log('starting');//<--------------------------------------- added line

var fs         = require('fs');
var request    = require('request');
var cheerio    = require('cheerio');

var htmlString = fs.readFileSync('scrap.html').toString();
var $          = cheerio.load(htmlString);
var json = [];

console.log('htmlString : ' + htmlString );//<-------------------- added line


$('body > table:nth-child(1) > tbody > tr:nth-child(3) > td > table > tbody > tr > td:nth-child(2) > table > tbody > tr > td > table > tbody > tr').each(function(i, element) {
    json.push({});
    json[i].range = $(this).text().trim();

});

console.log('Elements in json : ' + json.length);//<-------------- added line
console.log(json);

If this don't produce anything on the server-side, so yes we can confirm that your console.log don't work as expected, else it works and the problem come from other things !

Hope this will help you.