以流的概念html
步骤前端
pipenode
代码 const zlib = require( 'zlib' ) // zlib是一个压缩包的内置模块 const fs = require( 'fs' ) // fs是文件系统 const inp = fs.createReadStream('./dist/1.txt') // 建立可读的流 const out = fs.createWriteStream('1.txt.gz') //建立可写的流 const gzib = zlib.createGzip() // 建立一个空的压缩包 inp .pipe( gzib ) .pipe( out ) $ node 文件名
console模块web
经过后端语言爬取网站中的数据,而后经过特定模块进行数据清洗,最后将数据输出到前端json
不是全部的网站都能爬取后端
基本组成app
程序入口网站
请求模块ui
这边用的是get,代码以下: const http = require( 'http' ); const cheerio = require( 'cheerio' ); http.get('http://nodejs.org/dist/index.json', (res) => { const { statusCode } = res; // 获取状态码 1xx - 5xx const contentType = res.headers['content-type']; // 文件类型 text/json/html/xml let error; // 错误报出,状态码不是200,报错,不是json类型报错 if (statusCode !== 200) { error = new Error('Request Failed.\n' + `Status Code: ${statusCode}`); } else if (!/^application\/json/.test(contentType)) { error = new Error('Invalid content-type.\n' + `Expected application/json but received ${contentType}`); } if (error) { console.error(error.message); // consume response data to free up memory res.resume(); // 继续请求 return; } res.setEncoding('utf8'); // 字符编码
option里分别写入爬取网址的数据和请求头数据this
若是是html格式的,如下代码能够不用写
let error; // 错误报出,状态码不是200,报错,不是json类型报错 if (statusCode !== 200) { error = new Error('Request Failed.\n' + `Status Code: ${statusCode}`); } else if (!/^application\/json/.test(contentType)) { error = new Error('Invalid content-type.\n' + `Expected application/json but received ${contentType}`); } if (error) { console.error(error.message); // consume response data to free up memory res.resume(); // 继续请求 return; }
-数据解释
将爬取到的数据调用cheerio显示或保存
res.setEncoding('utf8'); // 字符编码 // 核心 -- start let rawData = ''; res.on('data', (chunk) => { rawData += chunk; }); // 数据拼接 res.on('end', () => { // 数据获取结束 try { const $ = cheerio.load( rawData ) $('td.student a').each( function ( item ) { console.log( $( this ).text() ) }) } catch (e) { console.error(e.message); } }); // 核心 -- end }).on('error', (e) => { console.error(`Got error: ${e.message}`); }); req.end()