我收到错误消息,因为 $ 不是函数
I am getting error as $ is not a function
var request = require("request"),
cheerio = require("cheerio"),
fs=require("fs"),
urls ,
url = "http://www.w3schools.com/";
request(url, function (error, response, body) {
if (!error && response.statusCode==200) {
var $ = cheerio.load(body).html();
var teli = $('a.w3schools-logo').html();
console.log(teli);
}
});
我收到错误消息
TypeError: $ is not a function
at Request._callback (C:\Users\AMIT\Desktop\project\demo.js:9:14)
at Request.self.callback (C:\Users\AMIT\Desktop\project\node_module \request\request.js:187:22)
at emitTwo (events.js:87:13)
at Request.emit (events.js:172:7)
at Request.<anonymous> (C:\Users\AMIT\Desktop\project\node_modules\request\request.js:1048:10)
at emitOne (events.js:77:13)
at Request.emit (events.js:169:7)
at IncomingMessage.<anonymous> (C:\Users\AMIT\Desktop\project\node_modules\request\request.js:969:12)
at emitNone (events.js:72:20)
at IncomingMessage.emit (events.js:166:7)
请帮忙解决这个错误
根据the cheerio page,正确的加载方式是:
var $ = cheerio.load(body); // <-- note no .html()!
将 .html()
returns 正文作为 HTML 字符串调用,这可能不是您想要的。相反,只需将其删除即可获得 jQuery 实例 $
.
您需要在正文加载后调用 html()
函数,如下所示
var request = require("request"),
cheerio = require("cheerio"),
fs=require("fs"),
urls ,
url = "http://www.w3schools.com/";
request(url, function (error, response, body) {
if (!error && response.statusCode==200) {
var $ = cheerio.load(body);
var teli = $('a.w3schools-logo').html();
console.log(teli);
}
});
var request = require("request"),
cheerio = require("cheerio"),
fs=require("fs"),
urls ,
url = "http://www.w3schools.com/";
request(url, function (error, response, body) {
if (!error && response.statusCode==200) {
var $ = cheerio.load(body).html();
var teli = $('a.w3schools-logo').html();
console.log(teli);
}
});
我收到错误消息
TypeError: $ is not a function
at Request._callback (C:\Users\AMIT\Desktop\project\demo.js:9:14)
at Request.self.callback (C:\Users\AMIT\Desktop\project\node_module \request\request.js:187:22)
at emitTwo (events.js:87:13)
at Request.emit (events.js:172:7)
at Request.<anonymous> (C:\Users\AMIT\Desktop\project\node_modules\request\request.js:1048:10)
at emitOne (events.js:77:13)
at Request.emit (events.js:169:7)
at IncomingMessage.<anonymous> (C:\Users\AMIT\Desktop\project\node_modules\request\request.js:969:12)
at emitNone (events.js:72:20)
at IncomingMessage.emit (events.js:166:7)
请帮忙解决这个错误
根据the cheerio page,正确的加载方式是:
var $ = cheerio.load(body); // <-- note no .html()!
将 .html()
returns 正文作为 HTML 字符串调用,这可能不是您想要的。相反,只需将其删除即可获得 jQuery 实例 $
.
您需要在正文加载后调用 html()
函数,如下所示
var request = require("request"),
cheerio = require("cheerio"),
fs=require("fs"),
urls ,
url = "http://www.w3schools.com/";
request(url, function (error, response, body) {
if (!error && response.statusCode==200) {
var $ = cheerio.load(body);
var teli = $('a.w3schools-logo').html();
console.log(teli);
}
});