zoukankan      html  css  js  c++  java
  • 爬虫http header gzip

    这个是常识!在request header里面加个gzip!可以大大的节约带宽。感谢来神在群聊天的时候说到这个。

    详情参见 wiki

    把昨天写的解析中关村在线的代码改了下

    /*
        Date: 2013-04-08
        Author: FangYuhao
    */
    var http = require('http');
    var iconv = require('iconv-lite');
    var cheerio = require('cheerio');
    var zlib = require('zlib');
    url = 'http://detail.zol.com.cn/332/331058/review.shtml'
    function parseZOL(data)
    {
        var $ = cheerio.load(data);
        //console.log($('.star_overview .nums').text());
        $('.comment_content').each(function(){
            console.log('good: '+$(this).children('dl').children('.good').next().text());
            console.log('bad: '+$(this).children('dl').children('.bad').next().text());
        });
    }
    var options ={
        host : 'detail.zol.com.cn' , 
        port : '80' ,
        path : '/332/331058/review.shtml' ,
        headers : {'Accept-Encoding' : ' gzip, deflate'}
    };
    http.get(options , function(res){
        var stack = '';
       // console.log(res.headers);
        var gunzip = zlib.createGunzip();
        res.pipe(gunzip);
        gunzip.setEncoding('binary');
        gunzip.on('data' , function(d){
            stack += d;
        }).on('error',function(err){
            console.log(err.message);
        });
    
        gunzip.on('end' , function(){
            var buf = new Buffer(stack ,'binary');
            var data = iconv.decode(buf , 'gbk');
            parseZOL(data);
        }).on('error',function(err){
            console.log(err.message);
        })
    }).on('error', function(err){
        console.log(err.message);
    });

    做个对比,下面这个是没有压缩

    然后这个是压缩了的

    效果明显!

    越来越觉得node写爬虫真的太方便鸟!

    by 1957
  • 相关阅读:
    三国演义中的十大谎言 VS 程序员十大口头禅
    Node.js 的单线程事件驱动模型和内置的线程池模型
    为了让医院把医护还给患者,他们用了这个…
    华为云数据库内核专家为您揭秘:GaussDB(for MySQL)并行查询有多快?
    brew
    Python
    Python
    Python
    AtCoder Beginner Contest 215 (个人题解 A~F)
    AcWing 第 13 场周赛  补题记录
  • 原文地址:https://www.cnblogs.com/x1957/p/3011104.html
Copyright © 2011-2022 走看看