【问题标题】:Nodejs jsdom encodingNodejs jsdom 编码
【发布时间】:2014-12-21 18:55:23
【问题描述】:

我正在尝试解析标题中带有以下标记的网站

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"

下一个词

阿弗伦茨 - Bürgeralm

我的节点 js 代码:

//includes

    var jsdom = require("jsdom");
    var fs = require('fs');
    var Buffer = require('buffer').Buffer;
    var Iconv  = require('iconv').Iconv;
    var iconv = new Iconv('iso-8859-1','utf-8');


 //parsing on callback from jsdom
        var name = $(".name_detail").html();
        console.log("db"+name);
        console.log("db"+iconv.convert(name).toString());

通过 ssh 输出:

dbAflenz - B�rgeralm
dbAflenz - B�rgeralm

提前谢谢

【问题讨论】:

  • 您的代码是否需要读取&lt;head&gt; 还是可以直接硬编码ISO-8859-1?你能展示你当前的解析 JSON 的代码吗?处理需要在该步骤中进行,而不是像您现在所做的那样。
  • 对不起,我不明白你的意思。我用 jsdom(后台请求)解析网站,然后将 jquery 添加到网站。

标签: node.js encoding iconv jsdom


【解决方案1】:

您可以尝试以下方法 (JSDOM allows to make the request in binary format):

request({uri: url, encoding: 'binary'}, function(err, request, body) {
     ...
     body = new Buffer(body, 'binary')
     iconv = new Iconv('ISO-8859-1', 'UTF8')
     body = iconv.convert(body).toString()

这将有助于解决转换问题。

【讨论】:

    猜你喜欢
    • 2016-09-11
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    • 2012-02-07
    • 2022-11-28
    • 1970-01-01
    相关资源
    最近更新 更多