为什么我使用utf-8搞乱代码?

时间:2015-05-05 02:46:31

标签: javascript node.js

我正在抓取一个将其字符集设置为utf-8的网站。但是当我抓取它时,当我抓取其他使用utf-8的网站时我的代码正常工作时返回了杂乱的代码。它有什么问题? 这是我的代码。

var http = require("http");
var iconv = require('iconv-lite');
var url = "http://www.zhihu.com";

//var url = "http://www.renren.com";
//var url = "http://www.baidu.com";

function download(url, callback) {
    http.get(url, function(res) {
        var data = "";
        res.on('data', function (chunk) {
            data += chunk;
        });
        res.on("end", function() {
            callback(data);
        });
    }).on("error", function() {
        callback(null);
    });
}


download(url, function(data) {
    if (data) {
        console.log(data);
    }
    else console.log("error");  
});

我期望得到的是这里。



<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="apple-itunes-app" content="app-id=432274380">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="renderer" content="webkit" />
<meta name="description" content="一个真实的网络问答社区,帮助你寻找答案,分享知识。"/>
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0"/>
<title>知乎 - 与世界分享你的知识、经验和见解</title>
<link rel="apple-touch-icon-precomposed" href="http://static.zhihu.com/static/img/ios/zhihu(57px).png" />
<link rel="apple-touch-icon-precomposed" href="http://static.zhihu.com/static/img/ios/zhihu(72px).png" sizes="72x72" />
<link rel="apple-touch-icon-precomposed" href="http://static.zhihu.com/static/img/ios/zhihu(76px).png" sizes="76x76" />
<link rel="apple-touch-icon-precomposed" href="http://static.zhihu.com/static/img/ios/zhihu(114px).png" sizes="114x114" />
<link rel="apple-touch-icon-precomposed" href="http://static.zhihu.com/static/img/ios/zhihu(120px).png" sizes="120x120" />
<link rel="apple-touch-icon-precomposed" href="http://static.zhihu.com/static/img/ios/zhihu(152px).png" sizes="152x152" />

<link rel="shortcut icon" href="http://static.zhihu.com/static/favicon.ico" type="image/x-icon" />
<link rel="dns-prefetch" href="p1.zhimg.com"/>
<link rel="dns-prefetch" href="p2.zhimg.com"/>
<link rel="dns-prefetch" href="p3.zhimg.com"/>
<link rel="dns-prefetch" href="p4.zhimg.com"/>
<link rel="dns-prefetch" href="comet.zhihu.com"/>
<link rel="dns-prefetch" href="static.zhihu.com"/>
<link rel="dns-prefetch" href="upload.zhihu.com"/>

<link rel="stylesheet" href="http://static.zhihu.com/static/ver/21cf1daf74b402877d664d7f82658789.reg.css" type="text/css" media="screen,print" />

<meta name="google-site-verification" content="FTeR0c8arOPKh8c5DYh_9uu98_zJbaWw53J-Sch9MTg" />
<meta property="qc:admins" content="14414345146201056375" />
<meta name="baidu-site-verification" content="KPFppAFoYF4Kkdv9" />

<link rel="canonical" href="http://www.zhihu.com" />

<meta id="znonce" name="znonce" content="b08b4ee57cd14fa39fcb08bd4f1c0673">
<!--[if lt IE 9]>
<script src="http://static.zhihu.com/static/components/respond/dest/respond.min.js"></script>
<link href="http://static.zhihu.com/static/components/respond/cross-domain/respond-proxy.html" id="respond-proxy" rel="respond-proxy" />
<link href="/static/components/respond/cross-domain/respond.proxy.gif" id="respond-redirect" rel="respond-redirect" />
<script src="/static/components/respond/cross-domain/respond.proxy.js"></script>
<![endif]-->

<script src="http://static.zhihu.com/static/ver/7ef789dcffd76ea3db4d1562e267ff99.instant.min.js"></script>
</head>
<body class="zhi">



<div class="wrapper index-content-wrapper">
<div class="top">
<div class="inner-wrapper">
<div class="form-wrapper" id="js-form-wrapper">
<div class="logo"></div>

<div id="js-sign-flow" class="desk-front sign-flow clearfix">
<div class="view view-signin ">
<form method="post" action="/login" class="zu-side-login-box">
<input type="hidden" name="_xsrf" value="0314c2fba8983a24e78e23d48647a8de"/>
<div class="title clearfix">
<a class="js-signup signup-switch with-icon" href="#signup">注册<i class="icon-sign icon-sign-arrow"></i></a>
<a class="with-icon return">
<i class="icon-sign icon-sign-arrow-l"></i><span class="js-title-label">登录知乎</span>
</a>
</div>
<div class="email input-wrapper">
<input required type="email" name="email" placeholder="知乎注册邮箱"

>
</div>
<div class="input-wrapper">
<input required type="password" name="password" maxlength="128" placeholder="密码">
</div>
<div class="captcha-holder">

</div>

<div class="button-wrapper command">
<button class="sign-button" type="submit">登录</button>
</div>
<div class="signin-misc-wrapper clearfix">
<label class="remember-me">
<input type="checkbox" name="rememberme" checked value="y"> 记住我
</label>
<a class="js-resetpassword reset-password" href="#">忘记密码?</a>
</div>
<div class="weibo-signup-wrapper">
社交帐号直接登录 &nbsp;
<a class="js-bindweibo" href="javascript:;"><i class="icon-sign icon-sign-sina"></i></a>
<a class="js-bindqq" href="javascript:;"><i class="icon-sign icon-sign-qq"></i></a>
</div>
</form>
</div>
&#13;
&#13;
&#13;

然而,这是它返回的乱码。

&#13;
&#13;
� �ZHU��[�s�ƕ���F�R�*����En9�U�8N��{x].V�ј��F �����rb]�Hi#ŶL[�c�PlJ��C�rU��h03��a_�!shf$�~�e�3D���~���n4����_��ƛ'^a*Q՝?r�~1.��s��
���Y�F�_U!WP�h��G6���fU��j5�pNT�H���`ߋ�%�8ǚ�%Q�dI��#+QT�ȩ��4����/q/����%�᯾2G�2����N˔zYijH@�ܘeb.:Q_O��8pj��{�΍���Ν�֍x����߶�������n}s����x}����xg;��Uc�f|w�y�!�j�:O�ϟm���u�v���Gk���KY��A��VI���Ĝ��fٱ�ʜE�L��b�q<'r��t�g���V�j�zؔ0���%�0��_7�i�l4v~ߺ�M&L%ʦw���ypg=����g�_��X)}���x�L@܎#��+���j�`?$�Tbϱ�nGK�0[����R��>YCɩ�K�����h���ּrj���I��B�4	�XM\����W�㫮h�s�+r�14����g-�}�E~��?k���д	�<�����C���FK��,|�L�Z� v��LJ+\J�W<��D6�p�á&P��2%ˎ�]���4Ywy��ЋD�ZcDQ�c��\Y�Cz���$��i�%�Dۂ�lM6e^�5�RU��l]T]Ӎـ�°cՈ�D���J,W���Nj2W�!y����ȡ�9��Q!�����:
~qⵊ����YY0�uC_8�S���"���5���Q�e�Z��H�α��QdU/��dY�%YdU�^Q%M�Kk&r��ȩ�v�x���q����k�֒�*y��܂Ɨ��sv�i:{�`H��,:W]�&��2!��-A��dؘ6Y�-`^�$Z��qo96�F̫�0��Вfh&�͞����R@B��*A��:���ٓ!;���8�X>5�8�0�,��������ډM���Φ�-��z'2!�ٔcٱ{�ƀ�Q��as�������[P}9��G]c�јՈ
�ia۶4� �2eKPT���fۆ1�!/d�RV�����`�!-[p*�9f9K��� r	����
�y'��R����Z���<������ڽ���d��4��w��Oe���l�٠�){����l�/8�"g0[�{���J�.�2���q<�ާ� h�*>��t�0�0<L����NS����r���w�$�*+�e��+a ����:\�� c�6�n�e��D�,YWe
�I�A^ô��unez�ט����PG.����g���~�]?�t�Ez���ǡ  ����Cy~]�0Q=���H�s3ba
y��'�qP����p/޿�V���Гz,��?�A�2���~� ��#��Iߎ!����0��.���ؔe���w�[�b��v<�5���V���5,G\��a���^�F|�l��v c�j`Uĥ}i��DD�ˬG��� U�Y�bz��/1L����aݬ��/3±Rzw�\�x��N�Y�|31g�s@��jTI1Bp��E9�%iڵJ�!�
�f��g�������W`N	�B ⒨�j&��5�;�PJu����k�8��V�ܰ�m�~�\�7��>�����T]�=3���;�3��a~-�����02jB�C�/#|��4TaT�f&v���@-�B$��BR$V*��u�F���~�ւ�(:50O���  ��4��$���]���Q��_^HY<�^���zf���p7�ԫ3�T�OD�T�0 Z��v�o;u�^���wT�A���(�"�˗���!��0IN�P���N�?T$��ׂSu���Q�!S��]�A�H����G�����^Lu<���a�Kk�!h�sDp.�R�I�h���*x��)���=�v�bJa"
�������NA���	'P`
A> ��`�Q1�N=�F��t	k�u��@nH�^�ɲӐ0>�t���3#�J�N
1١o�7�_N���9{�����������p�R{��!D��pZ��u�	�NKv�`�� ��Q���\��7��o^9X;��/�(�t���6XHVg�A� ��cu +�H���<�r��!S=9�Q>٨�m���ֹ�޾{���k�CIj�0�ie���6(�ҙr�Z[�5v.5w�}���70�7/}��Y��LAܩ��U�A+�=vb�=�
qʕh�����R��=�����nK����ȴ�t���+�!	��"�N�%��sI��������
<��a���u���˜:���מ&���L~�[%�fK�'��M��]�쓅�8$d fo/�H-�/L��!&BA�>l^0]�-v�,Ո˩��tř��92��K��dM�+]�w��E��ʆ �<o�X�T
aU�$�D�n�nhE��D��37���A����98�ԥ$��as�:{�Vf���#c	Z�}��{�V�K�D	yQ0ud(� #��&K�f˲����+�!��������"J�-���4"�v�U3��G<�,"芀�d&o	�"��&"�oy�x��+�;J����.K��M�F�pq��UN�H4C�u]�M�PM�4e�l�DǠaY�G��ƕ��Y1�`M#�q�EP7;�eUV'�N�M^EE4-[!D�T�Rx[�$ڢ������(A��k��e �������"+HP,ˀ��~����@�$@T��.�;��+vw�p����o�+�L$�X�'�'�*�+s'��0��z�a]�U�H��(�7����MdK��(�AI������=��ʢ�5���,���hLh�t�pZW����4.&j���� 1Q
��:o�V%ޠ����5�TO�9L&U2f�T��|ۺ��q�«6���
SM_������>�����qcF��eY��O35^t��dp'�
�������<!�N�l[�1F#�Y�gN�\G	(��N3%�w��]'����� �	
X��H���g��,A��
[!`������W��+��C�	�Dј8v�1�EpNT`y;n옖
Y���Ď&kP�L6�l�@{��~�ָ�ǪFŦ:E�pc�u���y YMQ��0�=��A���������9�qMcI6/ز�_u��G
O��<,mV���8�9�$]p
��%K(�wўށ�#���p�QX����J�;�Gr��tr�
�`۞5J���7�n�g?�/]lߺ�ܺqp�{z�49'���m}�ݼy������kW���������֗�@��'0��{�1��s�����O?x�`
d^c������r����Gk�_���i��:�΃�E��or��S���mɏ�6Q�7hk��j=U'!�A\��}�a��U^�a!���'	�jHP�?Z[o�����Mc����/���O���h�R|�^�{�`k�������i��n��Ŀ����(>�n���d����ۙRdX>?�E^T$�PbP��b5�.�S?X����\k����N��f���P&�O4v�ڷ��5�s�ʦ��3���ɪ�ve�5U��B>�����_5���k  ��
B��������I%��3S���M���OA���[�6���� ��^O�����pC�rIT�y����C����w���{�vf�*o�2b8��sWڟ\�� ԋ�.'���l~�A	2�o.��~���y��q?~�>M�e
7��>�/�����"�g1��6d3<����,K0�T�}k��rtۆ��S��"/(�r���"&�IOAL,���P$&LML��b2D���D�@L4���^$�g��������0!�P�M�i�!�k�/2�$S8|6z�z�~ݳ"��
��bo�m�ZgG|I�Rs� �śWZ���}�h���^|��x�*�ŧ�E#�pk�ۙ��o7G�ں�8&q�7[��f�W�G����̫/�`���1��qE����Jg��i�������_������a/�:OO����p)�I�<�^�{����ՠt���۟6��o��n�tF��J';~�*f��pH՜��cׁ$[��e���v�I'��h4��<+�k��l��g�=zn���;ќ��=y��rνFp2��C���a�$[�d��g�0v�-���~9!��g��s盧�B�%J����L�;3�s����9�Ss,��%Dc�?�w�p@=Xw��a=�=* C�#h��І�pp���E�(ˍ���'4Q�ѷx�$PU�vv���
0����{]�mI�
��� �Hu�n�nς2H�- �AG�'���6e�6��A�T��u(5���Yp���|��l��5��u� b�'�$��i���=K�������`5�Bǿ��l��kw��J晞W,�����~�J�� %��:  
&#13;
&#13;
&#13;

非常感谢!

1 个答案:

答案 0 :(得分:0)

感谢 mscdex ,我自己解决了这个问题。 我弄乱代码的原因是我以前不使用“zlib”! 这是我的代码,没有任何问题。 谢谢大家。

sum