【发布时间】:2015-03-06 17:00:50
【问题描述】:
使用request 模块加载网页时,我注意到对于英国英镑符号£,我有时会返回Unicode 替换字符\uFFFD。
我正在解析的示例 URL 是这个亚马逊英国页面:http://www.amazon.co.uk/gp/product/B00R3P1NSI/ref=s9_newr_gw_d38_g351_i2?pf_rd_m=A3P5ROKL5A1OLE&pf_rd_s=center-2&pf_rd_r=0Q529EEEZWKPCVQBRHT9&pf_rd_t=101&pf_rd_p=455333147&pf_rd_i=468294
我还使用iconv-lite 模块使用响应标头中返回的字符集进行解码:
request(urlEntry.url, function(err, response, html) {
const contType = response.headers['content-type'];
const charset = contType.substring(contType.indexOf('charset=') + 8, contType.length);
const encBody = iconv.decode(html, charset);
...
但这似乎没有帮助。我还尝试将响应 HTML 解码为UTF-8。
如何避免这种 Unicode 替换字符?
【问题讨论】:
标签: javascript node.js request