Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
9
rated 0 times [  11] [ 2]  / answers: 1 / hits: 10313  / 5 Years ago, wed, march 13, 2019, 12:00:00

I'm extracting the data from a page, but I get this error




TypeError: $ .find is not a function`




I already installed cheerio. When I put trm = $.find(.item-row[data-item='TRM']).find(.item-value > span); is when the error comes out, I get the data but this error comes out.



Code:



const express = require(express);
const app = express();
const https = require('https');
const cheerio = require('cheerio');

app.get('/', function(req, res) {
res.send('express test');
});

https.get('https://widgetsdataifx.blob.core.windows.net/semana/semanaindicators', (resp) => {
let data = '';

resp.on('data', (chunk) => {
data += chunk;
});

resp.on('end', () => {
var $ = cheerio.load(data);
trm = $.find(.item-row[data-item='TRM']).find(.item-value > span);
});

}).on(error, (err) => {
console.log(Error: + err.message);
});

More From » html

 Answers
5

There is no $.find() function, just as there isn't one in jQuery. There is a .find() method on jQuery objects, but that's not what $ represents.



trm = $(.item-row[data-item='TRM']).find(.item-value > span);


searches the markup loaded for item-row elements, and then from each of those it searches for <span> elements inside item-value elements.



As in real jQuery, the $ object is a function. You make functions calls to it and pass in selectors that you want Cheerio to find in the HTML markup you've loaded.



edit — here is a working test. If you npm install cheerio you can try it yourself with Node:



var cheerio = require(cheerio);

var $ = cheerio.load(`<body>
<div class=item-row data-item=TRM>
<div class=item-value>
<span>THIS IS THE CONTENT</span>
</div>
</div>
</body>`);


var span = $(.item-row[data-item='TRM']).find(.item-value > span);
console.log(span.text());

[#8474] Monday, March 11, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leslijessalyng

Total Points: 650
Total Questions: 85
Total Answers: 109

Location: Croatia
Member since Mon, Sep 6, 2021
3 Years ago
leslijessalyng questions
Fri, Feb 21, 20, 00:00, 4 Years ago
Tue, Jul 30, 19, 00:00, 5 Years ago
Fri, Jul 5, 19, 00:00, 5 Years ago
Fri, Dec 14, 18, 00:00, 6 Years ago
;