獲取的數據是亂碼,是怎么回事?
誰能幫我看看我的代碼,幫我看看,我是個初學者,弄了好久,還是沒弄出來,哪里出錯了???獲取到的數據不論是寫到文件,還是讀出來都是亂碼,就這個樣
var?http?=?require('http'); var?querystring?=?require('querystring'); var?Promise?=?require('Promise');?//?若版本過低,可使用bluebird替換 var?cheerio?=?require('cheerio');?//?類似于jQuery,安裝方法:npm?install?cheerio var?fs?=?require('fs'); var?path?=?require('path'); var?option={ hostname:?"user.pospal.cn", port:?80, path:"/Product/Manage", method:?"POST", headers:{ "Accept":?"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "Accept-Encoding":?"gzip,?deflate,?sdch", "Accept-Language":?"zh-CN,zh;q=0.8", "Cache-Control":?"max-age=0", "Connection":?"keep-alive", "Cookie":?"__ngDebug=true;?Vid=V2FQZl1mXj5QNQE6WWU=;?.POSPALAUTH30220=0102A061942D6F"?+ "84D308FEA0E15C7F6F8FD3080006350030003100350032003A0000012F00FFA5D1EE8E7CAB31C99"?+ "86D05F03CCD1198462091BE;?Hm_lvt_7d46a3151782b7a795ffeba367b5387d=1462767802,146"?+ "2771728;?Hm_lpvt_7d46a3151782b7a795ffeba367b5387d=1464480933;?Hm_lvt_18c160bec8"?+? "a91b8858c74ff702e6c788=1463498456,1463575485,1464061183,1464394742;?Hm_lpvt_18c"?+? "160bec8a91b8858c74ff702e6c788=1464513421;?ASP.NET_SessionId=10ooianyuuk3mcjwbhqs3rls", "Host":"user.pospal.cn", "Referer":?"http://user.pospal.cn/Product/CaseProductItem", "Upgrade-Insecure-Requests":?"1", "User-Agent":?"Mozilla/5.0?(Windows?NT?10.0;?WOW64)?AppleWebKit/537.36?(KHTML,?like?Gecko)?Chrome/50.0.2661.102?Safari/537.36", } }; var?req?=?http.request(option,?function?(res)?{ res.setEncoding('utf8'); var?data?=?''; console.log("Status:"?+?res.statusCode); console.log("headers:"?+?JSON.stringify(res.headers)); res.on('data',?function?(chunk)?{ console.log("is?Buffer:?"?+?Buffer.isBuffer(chunk)); console.log(typeof?chunk); data?+=?chunk; console.log(chunk.toString()); }); res.on('end',?function?()?{ console.log("shuju"?+?typeof?data); console.log("數據獲取完畢!"); fs.writeFile(path.join(__dirname,?'account.js'),?data,?(err)?=>?{ //fs.writeFile('account.js',?data,?(err)?=>?{ ??if?(err)?throw?err; ??console.log('It\'s?saved!'); }); filterHtml(data); }); }); req.on('error',?function?(e)?{ console.log("Error:?"?+?e.message); }); req.end();
2016-07-24
我已找到原因,為將來的自己和遇到同類問題的人作個參考,將解決方法列舉于此
我復制的是chrome瀏覽器發送的headers,其中有一個header頭"Accept-Encoding":?"gzip,?deflate,?sdch",告訴服務器支持的壓縮編碼方式(瀏覽器這樣做的目的是加快傳輸速度吧),然后我要爬取網站的服務器發來了"deflate"壓縮編碼的數據,而我的代碼中獲取到數據后未進行解碼,最后導致了亂碼。
解決方法:對數據使用nodejs的zlib模塊進行解碼或者去掉這個header頭,解碼的代碼大致如下
if (encoding == 'gzip') {
? ? ? ? ? ? zlib.gunzip(buffer, function (err, decoded) {
? ? ? ? ? ? if (err) {
? ? ? ? ? ? console.log(err);
? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? ? ? ? ? html = decoded.toString();
? ? ? ? ? ? ? ? Promise.resolve(html);
? ? ? ? ? ? });
? ? ? ? ? ??
? ? ? ? } else if (encoding == 'deflate') {
? ? ? ? ? ? zlib.inflateRaw(buffer, function (err, decoded) {// 注意異步問題
? ? ? ? ? ? if (err) {
? ? ? ? ? ? console.log(err);
? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? ? ? ? ? html = decoded.toString();
? ? ? ? ? ? });
? ? ? ? } else {
? ? ? ? ? ? html = buffer.toString();
? ? ? ? }
?PS:我這里之所以使用header頭,是我在登錄狀態下獲取瀏覽器的headers,這樣能爬取到登錄后顯示的數據,另外我是一個菜鳥
2016-07-21
我的代碼有沒有問題?誰幫我看看唄
第41行有時打印出false,什么情況?
2016-06-14
編碼格式:
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
2016-06-11
爬蟲程序漢字亂碼問題解答:http://www.xianlaiwan.cn/article/9053
2016-06-08
不對,還是不行
2016-05-30
var cheerio=require('iconv-lite')
var decodeBody=iconv.decode(Buffer.concat(html),'gbk');//html中的字符類型