gpt4 book ai didi

html - 使用 Node 的 http.get 获取 UTF-8 html 内容

转载 作者:搜寻专家 更新时间:2023-10-31 23:30:46 25 4
gpt4 key购买 nike

我正在尝试提取给定 url 的 html 内容,原始内容编码为 utf-8。我得到了页面的 html,但是 html 元素中的文本以错误的格式返回(问号)。

这是我的做法:

var parsedPath = url.parse(path);
var options = {
host: parsedPath.host,
path: parsedPath.path,
headers: {
'Accept-Charset' : 'utf-8',
}
}

http.get(options, function (res) {
var data = "";
res.on('data', function (chunk) {
data += chunk;
});
res.on("end", function () {
console.log(data);
});
}).on("error", function () {
callback(null);
});

如何强制对返回数据进行编码?

谢谢

最佳答案

使用 setEncoding()像这样的方法:

http.get(options, function (res) {
res.setEncoding('utf8');

var data = "";
res.on('data', function (chunk) {
data += chunk;
});
res.on("end", function () {
console.log(data);
});
});

关于html - 使用 Node 的 http.get 获取 UTF-8 html 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27998950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com