So I would appreciate some help on this one, tried a bit of everything.
let $ = cheerio.load("<html><table><tr><td>Example 1</td><td>Example 2</td></table>");
console.log($("td").text());
Gives this result (which is what I want):
// Example1Example2
However if I load a URL from a real web page, the result for the same selection is empty/null. This is the HTML for the page I load (not mine):
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<div class="middle"><img src="./example.jpg" /></div>
<table class="bgColor centered">
<thead>
<tr>
<th>Example header 1</th>
<th>Example header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Example 1</td>
<td>Example 2</td>
</tr>
</tbody>
</table>
</html>
And this is the code I run. There's no problem with the URL. I've tried different selectors but never got it right. Have been able to log some things but even though I use .text() it logs the whole html, with type, tag, etc... I'm probably missing something essential. But I left that out since I want the question to be simplified, and as far as I know I should be able to log the td's from this... Leaves the console log with two empty rows:
let myFoo = function(theUrl) {
return new Promise(function(resolve, reject) {
request(theUrl, function (error, response, html) {
if (error) {
return reject(error);
} else {
let $ = cheerio.load(html);
console.log($("td").text());
resolve();
}
});
});
};
via eiwob
No comments:
Post a Comment