admin管理员组文章数量:1025465
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.
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.
本文标签: javascriptTypeErrorfind is not a functionStack Overflow
版权声明:本文标题:javascript - TypeError: $ .find is not a function - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745632475a2160245.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论