javascript - 使用 Hapi、axios 和 Cheerio 时 JSON 输出中的编码错误
问题描述
有一个网站(我不管理)
<meta charset="ISO-8859-1">
我正在用cheerio,axios构建一个从这个网站读取一些数据的Hapi服务器
这就是网站显示我获得的数据的方式
<span class="name">DÓLAR BNA</span>
我将那个html加载到cheerio上
const response = await axios.get(url2Get);
const $ = cheerio.load(response.data.toString('ISO-8859-1'),{ decodeEntities: false });
// process with cheerio and build/send json response
但我仍然在 JSON 输出中得到这个
我也试过安装包 iconv-lite 并做
iconv.decode(Buffer.from(title), 'ISO-8859-1');
但还没有运气。感谢您的任何建议
解决方案
使用 axios 并将配置选项responseEncoding设置为“二进制”,如下所示:
const response = await axios.get(url2Get, {responseEncoding: 'binary'});
const $ = cheerio.load(response.data.toString('ISO-8859-1'),{ decodeEntities: false });
// process with cheerio and build/send json response
推荐阅读
- kubernetes - chown:更改“/var/lib/mysql/”的所有权:在 kubernetes 的启动 pod 中不允许操作
- user-interface - Vscode 缩进指南:如何减少(而不是隐藏)太多/激进的垂直线?
- python - 全局名称“a”是否与模块在同一个命名空间中?
- python - 如何使用turtle python进行可控的病毒模拟
- java - 应用程序启动方法中的 JavaFx 异常和应用程序启动方法中的异常
- node.js - nodejs express中的ajax xmlhttprequests
- python - 如何使用 Python 突出显示基于其他文本的单元格中的特定文本?
- c# - 如何在 C# 中打印 FlowLayoutPanel?
- javascript - 减少javascript中的嵌套对象
- angular - 使用 [ngTemplateOutlet] 在 ng-container 内的元素上应用类