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)的更多相关文章

  1. node.js 解析xml BOM问题(xmlreader sax.js)

    Email:longsu2010 at yeah dot net 之前写了两篇文章关于node.js解析xml,说的是xmlreader,文章如下 node.js解析xml(xmlreader) no ...

  2. Node.js抓取网页

    前几天四六级成绩出来(然而我没考),用Node.js做了一个模拟表单提交并抓取数据的Web 总结一下用到的知识,简单的网页抓取大概就是这个流程了 发送Get或Post请求 表单提交,首先弄到原网页提交 ...

  3. node.js之客户端发起https和http请求

    应用场景:1.VsCode插件开发(主要针对以javascript为主的vscode插件);2.使用Node.js开发的客户端程序 Node.js之http请求(客户端) 代码示例如下: var ht ...

  4. node.js解析微信消息推送xml格式加密的消息

    之前写过一个解密json格式加密的,我以为xml的和json的差不多,是上上个星期五吧,我的同事也是在做微信公众号里面的消息推送解密,发现好像只能使用xml加密格式的发送到服务器,我们去年也做过企业微 ...

  5. Node.js解析Excel

    1.使用node-xlsx包 var xlsx = require('node-xlsx'); 只支持xlsx格式 2.解析的Excel文件格式如下: 3.程序如下: var obj = xlsx.p ...

  6. node.js 抓取网页数据

    var $ = require('jquery'); var request = require('request'); request({ url: 'http:\\www.baidu.com',/ ...

  7. Node.js 部署免费/自动续订 HTTPS

    随着互联网快速发展,互联网信息安全越来越受到大家重视,HTTPS 应该是近两年各大厂商都在尽力普及的技术之一.国内大厂基本上已经全面普及了 HTTPS. 本文首发于我的个人网站:听说 - https: ...

  8. Node.js中的HTTPS示例

      需要openssl的支持, openssl本身不提供windows的安装程序,可以按照如下的步骤进行安装: (参考https://conetrix.com/Blog/how-to-install- ...

  9. Node.js meitulu图片批量下载爬虫1.06版

    //====================================================== // https://www.meitulu.com图片批量下载Node.js爬虫1. ...

随机推荐

  1. 如何修改linux 的SSH的默认端口号?

    http://blog.chinaunix.net/uid-7551698-id-1989086.html   在安装完毕linux,默认的情况下ssh是开放的,容易受到黑客攻击,简单,有效的操作之一 ...

  2. 错误”ORA-12560: TNS: 协议适配器错误“解决方法

    错误”ORA-12560: TNS: 协议适配器错误“解决方法 原本是可以通过sqlplus连接数据库的,命令如下: C:\Users\xuwei>sqlplus /nolog SQL*Plus ...

  3. pandas基础学习

    1.导入两个数据分析重要的模块import numpy as npimport pandas as pd2.创建一个时间索引,所谓的索引(index)就是每一行数据的id,可以标识每一行的唯一值dat ...

  4. Django基本操作

    Django官网下载页面 安装(安装最新LTS版): pip3 install django==1.11.9 创建一个django项目: 下面的命令创建了一个名为"s8"的Djan ...

  5. 使用graphql和apollo client构建react web应用

    graphql是一种用于 API 的查询语言(摘自官网). 我们为什么要用graphql? 相信大家在开发web应用的时候常常会遇到以下这些问题:后端更新了接口却没有通知前端,从而导致各种报错:后端修 ...

  6. [xampp] phpmyadmin 设置登录密码

    $ cd /opt/lampp/bin $ ./mysqladmin -u root password 'new_password' $ vim ../phpmyadmin/config.inc.ph ...

  7. ZCMU新人训练赛-B

     Tom's Meadow Tom has a meadow in his garden. He divides it into N * M squares. Initially all the sq ...

  8. Codeforces Round 254 (Div. 2)

    layout: post title: Codeforces Round 254 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  9. POJ1128 (TopSort)(递归)(回溯)

    Frame Stacking Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5220   Accepted: 1809 De ...

  10. Oracle SQL*Loader commit point tips

    http://www.dba-oracle.com/t_sql_loader_commit_frequency.htm - Question:  Can I control the commit fr ...