【问题标题】:iconv-lite not decoding everything properly, even though I'm using proper decodingiconv-lite 无法正确解码所有内容,即使我正在使用正确的解码
【发布时间】:2019-03-25 23:49:14
【问题描述】:

我正在使用这段代码下载网页(使用request 库)并解码所有内容(使用iconv-lite 库)。 loader 函数用于从网站正文中查找一些元素,然后将它们作为 JavaScript 对象返回。

request.get({url: url, encoding: null}, function(error, response, body) {
        // if webpage exists, process it, otherwise throw 'not found' error
        if (response.statusCode === 200) {
          body = iconv.decode(body, "iso-8859-1");
          const $ = cheerio.load(body);
          async function show() {
            var data = await loader.getDay($, date, html_tags, thumbs, res, image_thumbnail_size);
            res.send(JSON.stringify(data));
          }
          show();
        } else {
          res.status(404);
          res.send(JSON.stringify({"error":"No content for this date."}))
        }
      });

页面以 ISO-8859-1 格式编码,内容看起来正常,没有坏字符。当我不使用iconv-lite 时,一些字符,例如。 ü,看起来像这样:�。现在,当我使用上面提供的代码中的库时,大多数字符看起来都不错,但有些字符,例如。 š 是一个空框,即使它们在网站上显示没有任何问题。

我确定这不是cheerio 的问题,因为当我使用res.send(body);res.send(JSON.stringify({"body":body})); 打印输出时,空框字符仍然存在。也许是 Express 的问题?有办法解决吗?

编辑: 我将空框字符复制到谷歌,它已更改为š,也许这很重要

另外,我尝试使用 res.charset 更改 Express 的输出,但这没有帮助。

【问题讨论】:

    标签: node.js character-encoding iso-8859-1 iconv


    【解决方案1】:

    我用这个网站:https://validator.w3.org/nu/?doc=https%3A%2F%2Fapod.nasa.gov%2Fapod%2Fap170813.html 来检查我正在抓取的页面是否真的有ISO-8859-1 编码,结果它有Windows-1252 编码。我更改了我的 API (var encoding = 'windows-1252') 中的编码,现在它运行良好。

    【讨论】:

      猜你喜欢
      • 2016-07-18
      • 2020-11-26
      • 1970-01-01
      • 2016-06-15
      • 2012-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-27
      相关资源
      最近更新 更多