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 es out, I get the data but this error es 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('', (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);
});
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 es out, I get the data but this error es 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/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);
});
Share
Improve this question
edited Mar 13, 2019 at 13:20
adiga
35.3k9 gold badges65 silver badges87 bronze badges
asked Mar 13, 2019 at 13:12
Lucas SuárezLucas Suárez
331 gold badge1 silver badge5 bronze badges
9
-
1
@TusharWalzade that's almost certainly not the problem; the error message itself is pretty obvious: there is no
$.find()
method. Probably the OP just wants$(".item-row[data-item='TRM']")
etc – Pointy Commented Mar 13, 2019 at 13:15 - @brk it's Cheerio, a lightweight jQuery for Node scraping applications – Pointy Commented Mar 13, 2019 at 13:16
- Maybe "data" is not a valid HTML scaffolding – Mosè Raguzzini Commented Mar 13, 2019 at 13:16
- @TusharWalzade this is nodeJS server code, no jquery conflicts here. CheerioJS is used to replace jquery dom selectors – Mosè Raguzzini Commented Mar 13, 2019 at 13:17
-
1
Does it work if you use
trm = $(".item-row[data-item='TRM'] .item-value > span");
? – Salman Arshad Commented Mar 13, 2019 at 13:36
2 Answers
Reset to default 1There 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());
Playing with the code it looks like the var $ = cheerio.load( data )
expression assigns the $
variable to an instance of cheerio with the HTML document loaded. You then can traverse the dom like you would with jQuery.
Changing line 20 to
$("body").find(".item-row[data-item='TRM']").find(".item-value > span");
Will work because we are selecting the body
and then calling the find
method on the return value of the original query instead of the instance of cheerio itself.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745632476a4637190.html
评论列表(0条)