2016-09-29 74 views
2
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) 

請幫助我能爲這個錯誤

+0

你是否包括jQuery的身體後html()功能? – Gavin

+0

我安裝了jQuery作爲npm安裝jquery –

回答

3

the cheerio page做,正確的方式來加載它是:

var $ = cheerio.load(body); // <-- note no .html()! 

調用.html()回報體作爲一個HTML字符串,這很可能是不是你想要做的。相反,只要放下它即可獲得jQuery實例$

0

你需要調用加載如下

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); 
    } 
}); 
相關問題