模块请求如何正确检索重音字符? [英] Module request how to properly retrieve accented characters? � � �

查看:14
本文介绍了模块请求如何正确检索重音字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用:模块:请求 -- 简化的 HTTP 请求方法来抓取带有重音字符的网页áé ó ú ê ã

I'm using: Module: Request -- Simplified HTTP request method to scrape a webpage with accented characters á é ó ú ê ã etc.

我已经尝试过 encoding: utf-8 没有成功.我仍然在结果中得到这个 字符.

I've already tried encoding: utf-8 with no success. I'm still getting this ��� characters in the result.

request.get({
    uri: url,
    encoding: 'utf-8'
    // ...

是否有任何配置可以修复它?

Is there any configuration to fix it?

我不知道这是否有问题,但我为此模块填写了一个.还没有答案.:/

I don't know if it is an issue, but I filled one for this module. No answers yet. :/

推荐答案

由于不推荐使用二进制文件,因此使用 iconv 并正确处理解码似乎是一个更好的主意:

Since binary is deprecated it seems like a better idea to use iconv and correctly handle the decoding:

var request = require("request"), iconv  = require('iconv-lite');
var requestOptions  = { encoding: null, method: "GET", uri: "http://something.com"};

request(requestOptions, function(error, response, body) {
    var utf8String = iconv.decode(new Buffer(body), "ISO-8859-1");
    console.log(utf8String);
});

重要的部分是将HTTP请求的编码设置为null encoding: null.

The important part is to set the encoding on the HTTP request to be null encoding: null.

这篇关于模块请求如何正确检索重音字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆