我试图使用cheerio
来运行这个简单的scrape代码,但我一直都会遇到错误...我真的不知道问题是什么?请帮我 !
var request = require('request'),
cheerio = require('cheerio'),
url = "https://www.amazon.com/review/top-reviewers/ref=cm_cr_tr_link_2?ie=UTF8&page=1",
isFloat = ['Customer Reviewer', 'Total Reviews', 'Helpful Votes', 'Percent Helpful'];
exclude = ['Rank'];
require({url : url, time: true}, function (error, response, body) {
if (!error) {
var $ = cheerio.load(body),
result = {},
lastupdate = $(".a-bordered a-horizontal-stripes a-align- center a-spacing-top-none a-size-small crDataGrid.negmargin_lblUpdate").text(),
status = "Succsess",
message = "All data collected",
col = [];
result = {
status : status,
message : message,
data : {
lastupdate : lastupdate,
table : []
},
elapsedTime : response.elapsedTime
};
$('.a-bordered.a-horizontal-stripes.a-align-center.a-spacing-top-none.a-size-small.crDataGrid.neg-margin').each(function(i,tr) {
var row ={};
switch(i) {
case 0:
$(tr).find('th').each(function (j,th) {
col.push($(th).text().trim());
});
break;
default:
$(tr).find('td').each(function (j, td) {
if (exclude.indexOf(col[j]) !== -1) {
return true;
}
if (isFloat.indexOf(col[j]) !== -1) {
var match = $(td).text().trim().match(/[0-9,.]*/);
if (match !== null) {
row[col[j]] = parseFloat(match[0].replace(/,/g, '') );
} else {
row[col[j]] = parseFloat(match);
}
} else {
row[col[j]] = $(td).text().trim();
}
});
result.data.table.push(row);
break;
}
});
console.log(JSON.stringify(result));
} else {
console.log(JSON.stringify({status : "Failed", message : "We've encounted an error : " + error}));
}
});
这样的控制台错误,请帮我解决一下我的代码
assert.js:85
throw new assert.AssertionError({
^
AssertionError: path must be a string
at Module.require (module.js:496:3)
at require (internal/module.js:20:19)
at Object.<anonymous> (/home/fajrullah/Documents/Documents/pp/scraping/cek.js:7:1)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
答案 0 :(得分:1)
错误是您的require
声明。它需要一个字符串,因为require
是Node中的关键字。
我认为您打算使用request
包,因此请将行更改为:
request({url : url, time: true}, function (error, response, body) {
另请注意您的错误消息说明哪一行导致了问题:
at Object.<anonymous> (/home/fajrullah/Documents/Documents/pp/scraping/cek.js:7:1)
请查看该行并确定您获得AssertionError: path must be a string