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);
});
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.
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());