Node.js 解析gzip网页(https)
gzip网页指网页头字段Content-Encoding是gzip(GNU zip)内容编码方式。内容编码是指不丢失实体信息的前提下所进行的压缩。
Node.js 代码如下:
//====================================================
// 访问www.meitulu.com得到pagecode
// 2017年11月6日
//====================================================
// 内置https模块,提供了https服务器和客户端功能
var https=require("https");
var zlib = require('zlib');
// cheerio模块,提供了类似jQuery的功能
var cheerio = require("cheerio");
// 内置文件处理模块
var fs=require('fs');
// 请求参数JSON
var options;
// request请求
var req;
//--------------------------------------
// 程序入口 Accept-Encoding:gzip, deflate, br
//--------------------------------------
function start(){
// 初始化options
options={
hostname:'www.meitulu.com',
port:443,
path:'/item/40.html',// 子路径
method:'GET',
agent:false,
gzip: true,
};
req=https.request(options,function(resp){
var html = [];
resp.on("data", function(data) {
html.push(data);
})
resp.on("end", function() {
var buffer = Buffer.concat(html);
zlib.gunzip(buffer, function(err, decoded) {
console.log(decoded.toString());// gzip解压后的html文本
})
}).on("error", function() {
console.log("获取失败")
})
});
// 超时处理
req.setTimeout(5000,function(){
req.abort();
});
// 出错处理
req.on('error',function(err){
if(err.code=="ECONNRESET"){
console.log('socket端口连接超时。');
}else{
console.log('请求发生错误,err.code:'+err.code);
}
});
// 请求结束
req.end();
}
// 调用start函数,程序开始
start();
参考文档:
http://blog.csdn.net/u012935179/article/details/74022000
Node.js 解析gzip网页(https)的更多相关文章
- node.js 解析xml BOM问题(xmlreader sax.js)
Email:longsu2010 at yeah dot net 之前写了两篇文章关于node.js解析xml,说的是xmlreader,文章如下 node.js解析xml(xmlreader) no ...
- Node.js抓取网页
前几天四六级成绩出来(然而我没考),用Node.js做了一个模拟表单提交并抓取数据的Web 总结一下用到的知识,简单的网页抓取大概就是这个流程了 发送Get或Post请求 表单提交,首先弄到原网页提交 ...
- node.js之客户端发起https和http请求
应用场景:1.VsCode插件开发(主要针对以javascript为主的vscode插件);2.使用Node.js开发的客户端程序 Node.js之http请求(客户端) 代码示例如下: var ht ...
- node.js解析微信消息推送xml格式加密的消息
之前写过一个解密json格式加密的,我以为xml的和json的差不多,是上上个星期五吧,我的同事也是在做微信公众号里面的消息推送解密,发现好像只能使用xml加密格式的发送到服务器,我们去年也做过企业微 ...
- Node.js解析Excel
1.使用node-xlsx包 var xlsx = require('node-xlsx'); 只支持xlsx格式 2.解析的Excel文件格式如下: 3.程序如下: var obj = xlsx.p ...
- node.js 抓取网页数据
var $ = require('jquery'); var request = require('request'); request({ url: 'http:\\www.baidu.com',/ ...
- Node.js 部署免费/自动续订 HTTPS
随着互联网快速发展,互联网信息安全越来越受到大家重视,HTTPS 应该是近两年各大厂商都在尽力普及的技术之一.国内大厂基本上已经全面普及了 HTTPS. 本文首发于我的个人网站:听说 - https: ...
- Node.js中的HTTPS示例
需要openssl的支持, openssl本身不提供windows的安装程序,可以按照如下的步骤进行安装: (参考https://conetrix.com/Blog/how-to-install- ...
- Node.js meitulu图片批量下载爬虫1.06版
//====================================================== // https://www.meitulu.com图片批量下载Node.js爬虫1. ...
随机推荐
- td中嵌套table,让table完全填充父元素td
<table width="100% " cellspacing=0 cellpadding=0 border=1 > <tr> <td style= ...
- webpack-dev-server坑
转载人家滴 https://segmentfault.com/q/1010000007561947/a-1020000007596130 需要webpack开发服务(webpack-dev-serve ...
- UVALive - 5844
题是pdf版 Sample Input23mississippinni55i55ippi2foobar|=o08arSample Output10 /** 题意:给出一个normal串,一个leet串 ...
- 详解Python中的__new__、__init__、__call__三个特殊方法(zz)
__new__: 对象的创建,是一个静态方法,第一个参数是cls.(想想也是,不可能是self,对象还没创建,哪来的self)__init__ : 对象的初始化, 是一个实例方法,第一个参数是self ...
- runtimeService.startProcessInstanceById("process:6:55036", 2222, variables) SQL语句
JAVA: variables:{ user_flow_start_dept : "3333"} runtimeService.startProcessInstanceById(& ...
- [libgdx游戏开发教程]使用Libgdx进行游戏开发(6)-添加主角和道具
如前所述,我们的主角是兔子头.接下来我们实现它. 首先对AbstractGameObject添加变量并初始化: public Vector2 velocity; public Vector2 term ...
- nodejs获取ASP.Net WebAPI(IIS Windows验证)
处理了很多天,终于使用Nodejs可以发出请求至WebAPI,能够正常处理数据了 首先加入npm包 npm install httpntlm 在app.js中加入代码 var httpntlm = r ...
- python读取大文件【一行一行读取】
with open('e:/content.txt') as f: for line in f: if '==3346628==' in line: …………
- UVA548 Tree (二叉树的遍历)
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- 【树上莫队】【带修莫队】bzoj3052 [wc2013]糖果公园
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using ...