【问题标题】:nodejs encoding using request使用请求的nodejs编码
【发布时间】:2012-08-20 15:31:53
【问题描述】:

我正在尝试通过请求获得正确的编码。

request.get({
    "uri":'http://www.bold.dk/tv/',
    "encoding": "text/html;charset='charset=utf-8'"
  },
  function(err, resp, body){    
    console.log(body);
  }
);

无论我做什么,丹麦字符的编码都不正确。

有什么想法吗?

【问题讨论】:

  • 您将encodingcontent-type 标头混合在一起——例如:"encoding": "utf-8"。但是,页面编码为ISO-8859-1,而不是UTF-8。为此,请参阅stackoverflow.com/questions/8915404/…
  • @Amberlamps:我正在使用记事本++
  • @hippie:现在这是一个长镜头,但有时我对德语字母也有同样的问题。每次发生这种情况都是因为我的 Notepad++ 将我的脚本保存为 ANSI 而不是 UTF-8。如果它在 ANSI 中,请尝试将其切换为 UTF-8。这是 Coding 下 Notepad++ 中的一个选项(不知道英文术语是什么,因为我使用的是德语版本)
  • 混合起来,尝试了很多东西。我都试过了,没有任何效果。
  • @Amverlambs:刚刚试过,没用。

标签: node.js encoding


【解决方案1】:

您可以使用 iconv (lite) 来转换它。您还需要通过将 encoding 属性设置为 null 来告诉 request 不要主动将编码设置为 UTF-8 的默认值。因此你应该这样做:

var iconv = require('iconv-lite');
request.get({
    uri:'http://www.bold.dk/tv/',
    encoding: null
  },
  function(err, resp, body){    
    var bodyWithCorrectEncoding = iconv.decode(body, 'iso-8859-1');
    console.log(bodyWithCorrectEncoding);
  }
);

【讨论】:

    【解决方案2】:

    也许你的麻烦在于'Accept-Encoding' 标头。 假设你有像'Accept-Encoding': 'gzip,deflate'这样的标题

    如果是这样,您有两种方法可以解决此问题:

    1. 删除此标题
    2. 使用以下代码解压数据:

      const req = request(options, res => {
          let buffers = []
          let bufferLength = 0
          let strings = []
      
          const getData = chunk => {
              if (!Buffer.isBuffer(chunk)) {
                  strings.push(chunk)
              } else if (chunk.length) {
                  bufferLength += chunk.length
                  buffers.push(chunk)
              }
          }
      
          const endData = () => {
              let response = {code: 200, body: ''}
              if (bufferLength) {
                  response.body = Buffer.concat(buffers, bufferLength)
                  if (options.encoding !== null) {
                      response.body = response.body.toString(options.encoding)
                  }
                  buffers = []
                  bufferLength = 0
              } else if (strings.length) {
                  if (options.encoding === 'utf8' && strings[0].length > 0 && strings[0][0] === '\uFEFF') {
                      strings[0] = strings[0].substring(1)
                  }
                  response.body = strings.join('')
              }
              console.log('response', response)
          };
      
          switch (res.headers['content-encoding']) {
              // or, just use zlib.createUnzip() to handle both cases
              case 'gzip':
                  res.pipe(zlib.createGunzip())
                      .on('data', getData)
                      .on('end', endData)
                  break;
              case 'deflate':
                  res.pipe(zlib.createInflate())
                      .on('data', getData)
                      .on('end', endData)
                  break;
              default:
                  res.pipe(zlib.createInflate())
                      .on('data', getData)
                      .on('end', endData)
                  break;
          }
      });
      

    【讨论】:

    • 从请求标头中删除 'accept-encoding' 为我解决了这个问题。谢谢。
    【解决方案3】:

    我也有同样的问题,request v2.88.0

    参考woolfi makkinan的回答,我有一个简单的方法来解决这个问题。

    request.get({
        "uri": 'http://www.bold.dk/tv/',
        "encoding": "text/html;charset='charset=utf-8'",
        "gzip": true // notice this config
      },
      function(err, resp, body){    
        console.log(body);
      }
    );
    

    gzip: true添加到request选项,request会处理gzip,然后blob可以正确转换为字符串。 ​

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-19
      • 2017-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-26
      相关资源
      最近更新 更多