iconv-lite 没有正确解码所有内容,即使我使用了正确的解码
iconv-lite not decoding everything properly, even though I'm using proper decoding
我正在使用这段代码下载网页(使用 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的问题?有办法解决这个问题吗?
编辑:
我把空框字符复制到Google,它变成了š
,也许这很重要
此外,我尝试使用 res.charset
更改 Express 的输出,但这没有帮助。
我使用这个网站: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'
) 中的编码,现在它运行良好。
我正在使用这段代码下载网页(使用 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的问题?有办法解决这个问题吗?
编辑:
我把空框字符复制到Google,它变成了š
,也许这很重要
此外,我尝试使用 res.charset
更改 Express 的输出,但这没有帮助。
我使用这个网站: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'
) 中的编码,现在它运行良好。