javascript - TypeError: $ .find is not a function - Stack Overflow

I'm extracting the data from a page, but I get this error TypeError: $ .find is not a function`I a

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
 |  Show 4 more ments

2 Answers 2

Reset to default 1

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

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

相关推荐

  • javascript - TypeError: $ .find is not a function - Stack Overflow

    I'm extracting the data from a page, but I get this error TypeError: $ .find is not a function`I a

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信