• NodeJs+http+fs+request+cheerio 采集,保存数据,并在网页上展示(构建web服务器)


    目的:

      数据采集

      写入本地文件备份

      构建web服务器

      将文件读取到网页中进行展示

    目录结构:

    package.json文件中的内容与上一篇一样:NodeJs+Request+Cheerio 采集数据

    request :https://github.com/request/request 使得请求变得更容易,简单

    cheerio:https://github.com/cheeriojs/cheerio 用来解析dom结构,类似jQuery,挺好用

    app.js文件:

    /**
     * 数据采集
     * 写入本地文件备份
     * 创建web服务器
     * 将文件读取到网页中进行展示
     */
    //引入需要的包
    var http = require('http');
    //var path = require('path');
    var request = require('request');
    var cheerio = require('cheerio');
    var fs = require('fs');
    
    //定义常量
    var dolphin = 'http://cn.dolphin.com/blog';
    const filePath = '/NodeJsTest/test_7/sampleCollection/localFiles/opts.txt';
    
    //数据请求
    function dataRequest(dataUrl) {
        //发送请求
        request({
            url : dataUrl,
            method : 'GET'
        },function(err, red, body) {
            //请求到body
            if(err){
                console.log(dataUrl);
                console.error('[ERROR]Collection' + err);        
                return;
            }
    
            if(dataUrl && dataUrl === dolphin){
                dataPraseDolphin(body);
            }
        })
    }
    
    /**
     * 解析html
     */
    function dataPraseDolphin(body) {
        
        var $ = cheerio.load(body);
    
        var atricles = $('#content').children('.status-publish');
    
        for(var i = 0;i < atricles.length;i++){
            var article = 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();
    
            var localData;
    
            if($p){
                localData = '--------------'+ (i+1) +' Chapter------------------' + '
    '
                          + '标题:' + $aVal + '
    '
                          + '简介:' + $pVal + '
    '
                          + '时间:' + new  Date + '
    '
                          + '---------------------------------------------------' + '
    ';
                console.log(localData);
                writeToLocal(localData,i);
            }
    
    
        }
    }
    
    /**
     * [writeToLocal description]
     * 将解析的数据 写入本地文件进行备份
     */
    function writeToLocal(dataPage,fj){
        console.log('-------------准备写入文件------------------------')
        //同步写入文件,一般使用异步好
        fs.appendFileSync(filePath, dataPage);
    }
    
    /**
     * 创建web服务器
     * @return {[type]} [description]
     */
    function createServer(){
        http.createServer(function(req,resp){
    
            console.log('服务启动!')
            wirteToPage(resp);
            
        }).listen(7000);
    }
    
    /**
     * 将抓取的数据写入页面
     */
    function wirteToPage(resp){
        fs.readFile(filePath,function(err,data){
            if(err){
                console.log(err);
                resp.writeHead(404,{
                    'Content-Type':'text/html'
                })
            }else{
                resp.writeHead(200,{
                    //响应头添加编码格式解决乱码问题
                    'Content-Type': 'text/plain;charset=utf-8'
                });
                //resp.write('<head><meta charset="utf-8"/></head>');      
                resp.write(data.toString());
            }
            resp.end();
        })
    }
    
    //开始发送请求 并 采集数据
    dataRequest(dolphin);
    createServer();

    Sublime 中 ctrl+B 执行 

    浏览器地址栏请求:http://localhost:7000

     结果

     

  • 相关阅读:
    Bootstrap3.0入门学习系列教程
    Python——五分钟理解元类(metaclasses)
    Python内置模块和第三方模块
    vs的任务列表
    web.config or app.config 中configSections配置节点
    将查询到的数据导出到Excel终结版
    MVC的一些常用特性,持续更新中。。。
    前端——扫码枪扫码
    前后台数组互传问题解答
    C# 实体/集合差异比较,比较两个实体或集合值是否一样,将实体2的值动态赋值给实体1(名称一样的属性进行赋值)
  • 原文地址:https://www.cnblogs.com/zqzjs/p/5487381.html
Copyright © 2020-2023  润新知