Answer the question
In order to leave comments, you need to log in
Why is it giving an empty result when parsing a web page?
outputs an empty string to the console
const request = require("request")
const cheerio = require("cheerio");
var html = request('https://google.com', {
'Content-type': 'text/html'
});
const $ = cheerio.load(html);
const a = $('body').text();
console.log(a);
Answer the question
In order to leave comments, you need to log in
in html, not html, but a request object
request('https://google.com', function (error, response, html) {
if (!error && response.statusCode == 200) {
const $ = cheerio.load(html);
const a = $('body').text();
console.log(a);
}
});
You need to get a little better at debugging your scripts.
For example, it's trite, but console.log(a);
you can add console.log(html);
it right after something should appear in this variable. If it appears, then something is wrong after this line. If it does not appear, then something is wrong up to this line. Of the various errors, we immediately remove half.
Thus, by the elimination method (that is, eliminating half of the errors each time), you can get to the place where the error is, and then it will be easy to understand it.
The debugging process is only available to someone who has the ability to run the script under the conditions where the error occurs. All other people can only guess what the matter is. Therefore, it is best not to ask questions where debugging is needed to get an answer. Besides, it's a job that just needs to be done and doesn't require an expert.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question