Objective:
Data acquisition
Write Local file backup
Building a Web server
Reading a file to a Web page for presentation
Directory structure:
The contents of the Package.json file are the same as in the previous article: Nodejs+request+cheerio Data acquisition
Request:https://github.com/request/request makes requests easier and easier
Cheerio:https://github.com/cheeriojs/cheerio is used to parse the DOM structure, like jquery, very useful
App.js file:
/** * Data acquisition * Write local file backup * Create Web server * Read file to Web page to show*///introduce the required packagesvarHTTP = require (' http ');//var path = require (' path ');varRequest = require (' request '));varCheerio = require (' Cheerio '));varFS = require (' FS ');//Defining ConstantsvarDolphin = ' Http://cn.dolphin.com/blog '; Const FilePath= '/nodejstest/test_7/samplecollection/localfiles/opts.txt ';//Data RequestfunctionDatarequest (dataurl) {//Send Requestrequest ({Url:dataurl, method:' GET ' },function(err, red, body) {//Request to Body if(Err) {console.log (Dataurl); Console.error (' [error]collection ' +err); return; } if(Dataurl && Dataurl = = =Dolphin) {Dataprasedolphin (body); } })}/** * Parsing HTML*/functionDataprasedolphin (body) {var$ =Cheerio.load (body); varAtricles = $ (' #content '). Children ('. Status-publish ')); for(vari = 0;i < atricles.length;i++){ varArticle =Atricles[i]; var$a = $ (article). Find ('. Post-title. Entry-title a '); var$p = $ (article). Find ('. Post-content p '); var$aVal =$ ($a). text (); var$pVal =$ ($p). text (); varLocaldata; if($p) {Localdata= '--------------' + (i+1) + ' Chapter------------------' + ' \ n ' + ' title: ' + $aVal + ' \ n ' + ' Introduction: ' + $pVal + ' \ n ' + ' time: ' +NewDate + ' \ n ' + '---------------------------------------------------' + ' \ n '; Console.log (Localdata); Writetolocal (Localdata,i); } }}/** [writetolocal description] * writes resolved data to a local file for backup*/functionwritetolocal (DATAPAGE,FJ) {Console.log ('-------------ready to write to file------------------------') //synchronous writing of files, generally using asynchronous goodFs.appendfilesync (FilePath, dataPage);}/** * Create Web server * @return {[type]} [description]*/functionCreateserver () {Http.createserver (function(REQ,RESP) {Console.log (' Service started! ‘) Wirtetopage (RESP); }). Listen (7000);}/** * Write crawled data to the page*/functionWirtetopage (resp) {fs.readfile (FilePath,function(err,data) {if(Err) {Console.log (err); Resp.writehead (404,{ ' Content-type ': ' text/html ' }) }Else{Resp.writehead (200,{ //response Header Add encoding format to solve garbled problem ' content-type ': ' Text/plain;charset=utf-8 ' }); //resp.write (' Resp.write (data.tostring ()); } resp.end (); })}//start sending requests and collecting datadatarequest (Dolphin); Createserver ();
Sublime in ctrl+b execution
Browser address bar Request: http://localhost:7000
Results :
Nodejs+http+fs+request+cheerio capture, save data, and display it on a Web page (build a Web server)