//server.js
1 varHTTP = require (' http '),2url = require ("url"),3Superagent = require ("superagent"),4Cheerio = require ("Cheerio"),5Async = require ("Async"),6Eventproxy = require (' Eventproxy ')),7FS = require ("FS"),8Nodeexcel = require (' Excel-export ')),9QueryString = Require (' querystring '));Ten Oneexports.start=function() { A //HTTP request from the request client//response server-side HTTP response - functionOnreqoest (req,response) { - //Set character encoding (remove Chinese will garbled, respond) theResponse.writehead, {' Content-type ': ' Text/html;charset=utf-8 '}); - superagent -. Get (' https://list.yohobuy.com/boys-new/') -. Set (' Accept ', ' Application/json ') +. End (function(err,pres) { - varStartDate =NewDate (); + //General error Handling A if(err) { atConsole.log (1) - return; - } - varHtml=Pres.text; - varChapterdata; - varChapter = Crawlerchapter (HTML);//2 in //Console.log (chapter); - Console.log (chapter); to functionCrawlerchapter (HTML) { + var$ =cheerio.load (HTML); - varChapters = $ ('. Good-info ')); the varData1 = []; *Chapterdata = { $ imgurl:[],Panax Notoginseng Biaoti: [], - price:[] the }; +$ ('. Good-detail-img '). each (function(i, elem) { AChapterdata.imgurl[i] = $ ( This). text (); the }); +$ ('. Good-detail-text>a '). each (function(i, elem) { -Chapterdata.biaoti[i] = $ ( This). Text (). Trim (); $ }); $$ ('. Good-detail-text. Market-price '). each (function(i, elem) { -Chapterdata.price[i] = $ ( This). Text (). Trim (); - }); the }; - varconf= {},temp=[];WuyiConf.cols =[ the{caption: ' title ', -Type: ' String '}, Wu{caption: ' Price ', -Type: ' String '}, About{caption: ' Picture address ', $Type: ' String '} - ]; - for(vari=0; i<chapterdata.biaoti.length;i++) { - varBuffer =[Chapterdata.biaoti[i], chapterdata.price[i]]; A console.log (buffer); + temp.push (buffer); the } -Conf.rows=temp; $ varresult =nodeexcel.execute (conf); the varRandom = Math.floor (Math.random () *10000+0);//to ensure that different file names are generated the varFilePath = "fi" + Random + ". xlsx";//file name theFs.writefile (FilePath, result, ' binary ',function(err) { the if(err) { - Console.log (err); in } the }); the }); About } theHttp.createserver (Onreqoest). Listen (3500); the}
App.js
var//node JS creates a module server.start ();
Write for the first time, find a Web page, and then just fine!!!
Do you have any opinion to be published Oh .... , I want to progress!.
node. JS Crawler and data export to Excel