0

请求的接口是:"http://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98vv125475&productId=1993092&score=0&sortType=5&page=2&pageSize=10&isShadowSku=0&rid=0&fold=1"
网页显示中文正常,返回来的数据中文乱码

const http=require("http");
const fs=require("fs")
// const cheerio=require("cheerio")
// const iconv=require("iconv");
var url="http://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98vv125475&productId=1993092&score=0&sortType=5&page=2&pageSize=10&isShadowSku=0&rid=0&fold=1";

http.get(url,function (res) {
    var html="";
    // iconv.decode(res, 'gb2312')
    res.setEncoding("utf-8");
    res.on("data",function(chunk){

        //console.log(chunk.toString())
        html+=chunk;
    });
    res.on("end",function(){
        fs.writeFile("./评价.txt",a,function(){
            console.log("完成")
        })
    });
    res.on("err",function(){
        console.log(err)
    })
})

2个回答

1
var superagent = require('superagent');
var charset = require('superagent-charset');
var fs = require('fs');
superagent = charset(superagent);
superagent
    .get('http://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98vv125475&productId=1993092&score=0&sortType=5&page=2&pageSize=10&isShadowSku=0&rid=0&fold=1')
    .charset("gbk")
    .set('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36')
    .end(function(err, res) {
        // console.log(err);
        fs.writeFile("./评价.txt",JSON.stringify(res.text),function(){
            console.log("完成")
        })
    })

用的superagentsuperagent-charset模块。

0

顺着楼主的思路来,对代码进行了微调,主要修改点在用了iconv-lite进行了转码操作。代码如下:

const http=require("http");
const fs=require("fs")
var url="http://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98vv125475&productId=1993092&score=0&sortType=5&page=2&pageSize=10&isShadowSku=0&rid=0&fold=1";
var iconv = require('iconv-lite');

http.get(url,function (res) {
    
    var arr = [];
    var chunks;

    res.on("data",function(chunk){
        arr.push(chunk);
    });
    
    res.on("end",function(){
        chunks = Buffer.concat(arr);
        var body = iconv.decode(chunks, 'gbk');
        
        fs.writeFile("./评价.txt", body, function(){
            console.log("完成")
        });
    });
    
    res.on("err",function(){
        console.log(err)
    })
});

也可以考虑采用比较简便的版本:

var http=require("http");
var fs = require('fs');
var iconv = require('iconv-lite');

var url="http://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98vv125475&productId=1993092&score=0&sortType=5&page=2&pageSize=10&isShadowSku=0&rid=0&fold=1";
var dest = './评价.txt';
var charset = 'gbk';

http.get(url, function (res) {
    var converterStream = iconv.decodeStream(charset);
    var destStream = fs.createWriteStream(dest);
    res.pipe(converterStream).pipe(destStream);
});

撰写答案