Node.js应用程序冻结

我正在使用带有cheeriorequest模块的node.js进行抓取。 我想刮二十多万内容。 我写了一个请求内容列表的脚本,然后请求每个内容并将其保存到数据库。

到目前为止一切正常。 我已经testing了保存1000个内容的脚本,没有看到任何错误或错误发生。

然后,我决定运行脚本来抓取所有的内容。 运行之后,出现10000个内容后出现问题。 我的应用程序开始冻结! 它刚刚停止,并没有显示任何错误。 我检查了CPU和内存的使用情况 – 这是正常的,mongodb是好的,一切都很好。 脚本停止工作。

这里是我用于抓取的代码:

 var count = 1; async.whilst( function () { return count < 2412; }, function (callback) { request({ url:"http://www.domain.com/limit/100/page/"+count, method:"GET" }, function(error,response,body){ if (!error && response.statusCode == 200) { if(body){ $ = cheerio.load(body); $('.linear_news a').each(function(item){ console.log($(this).attr('href')) if($(this).attr('href')){ request({ url:encodeURI("http://www.domain.com"+$(this).attr('href')), method:"GET" }, function(error,response,body1){ if (!error && response.statusCode == 200) { if(body){ $ = cheerio.load(body1); var title = $('.title a').text(); if($('head > base').attr('href')){ var postId =$('head > base').attr('href').match(/\/fa\/news\/(\d+)/); } var postContent = $('.body').html(); var post = { title:title, postId:postId[1], content:postContent, created:new Date() } savePost(post,function(err,msg){ if(err){ console.log(err); }else{ console.log(msg); } }) // save tags $('.tags_item').each(function(item){ var myData = { tagName:$(this).text(), created:new Date() } saveTags(myData,function(err,msg){ if(err){ console.log(err) }else{ console.log(msg) } }) })//end save tags // save images $('.body img').each(function(item){ var image = $(this).attr('src'); var myImages = { image:image, alt:title, imageID:imageID, created:new Date() } saveImages(myImages,function(err,msg){ if(err){ console.log(err) }else{ console.log(msg) } }) })// end of save images } } }) // end of request for tags }// end of ($(this).attr('href')) }) } callback(); } })// emd of requset for news list count++; }, function (err) { console.log("shit is done") } );