nodejs 报错errno: -4077,code: 'ECONNRESET'

学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')

执行到四十多章的时候就报错了:
错误代码
求解答
非常感谢!

阅读 10.1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题