首页 技术 正文
技术 2022年11月14日
0 收藏 523 点赞 4,482 浏览 2120 个字

目的:

  数据采集

  写入本地文件备份

  构建web服务器

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

目录结构:

NodeJs+http+fs+request+cheerio 采集,保存数据,并在网页上展示(构建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------------------' + '\n'
+ '标题:' + $aVal + '\n'
+ '简介:' + $pVal + '\n'
+ '时间:' + new Date + '\n'
+ '---------------------------------------------------' + '\n';
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

结果:

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

上一篇: eclipse svn 配置
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289