学nodejs,想写学着写个爬虫,这是代码:
function getText(url) {
superagent.get(url, function (err, res) {
if (err) {
console.error(err)
return
}
const $ = cheerio.load(res.text);
if($('#next_url').length) {
if($('#next_url').text() === '下一章') {
num++
text += `\n难哄 第${num}章\n`
console.log(`正在爬取${num}章`)
}
setTimeout(() => {
console.log($('#next_url').attr('href'))
getText(baseUrl + $('#next_url').attr('href'))
},1000)
} else {
console.timeEnd("共花费了");
// fs.writefile(`${__dirname}/难哄.txt`, text, 'utf8', function (error) {
// if (error) {
// console.log(error);
// return false;
// }
// console.log('写入成功');
// })
}
text += $('#booktxt').text();
fs.appendFile(`${__dirname}/难哄.txt`, text, 'utf8', function (error) {
if (error) {
console.log(error);
return false;
}
text = ''
console.log('写入成功');
})
})
}
getText('https://www.nantunwang.com/book/50408/33491665.html')
执行到四十多章的时候就报错了:
求解答
非常感谢!