Nodejs+http+fs+request+cheerio capture, save data, and display it on a Web page (build a Web server)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.