Nodejs crawler抓取内容出现乱码了

Nodejs crawler抓取内容出现乱码了

exports.index = function(req, res){ var Crawler = require(“crawler”).Crawler;

var c = new Crawler({ “maxConnections”:10, “debug”:true, “forceUTF8”:true, // This will be called for each crawled page "callback":function(error,result,$) {

    // $ is a jQuery instance scoped to the server-side DOM of the page
var te=$("#top .gengxin table  tr:first").html();
console.log(te);
 res.render('index',{title:te});

}

}); c.queue(“http://psv.tgbus.com”); };

后台输出: GET http://psv.tgbus.com … Got http://psv.tgbus.com (107044 bytes)… forceUTF8 true Detected charset windows-1252 (95% confidence) <td width=“10” valign=“top” class="">茂驴陆茂驴陆</td><td class=""><a class="" href=“http://psv.tgbus.com/yxgl/201303/20130314105417.shtml” title=“茂驴陆茂驴陆茂驴陆茂驴陆茂驴陆茂驴陆茂驴陆寐济柯矫柯矫柯矫柯矫陆茂驴陆寐柯矫柯矫柯矫柯?nbsp;茂驴陆茂驴陆茂驴陆茂驴陆茂驴陆茂驴陆茂驴陆寐济柯矫柯矫柯矫柯矫柯矫柯矫柯? target=”_blank"><font color="#FF0000">茂驴陆茂驴陆茂驴陆茂驴陆茂驴陆茂驴陆茂驴陆寐济柯矫柯矫柯矫柯矫陆茂驴陆寐柯矫柯矫柯矫柯?nbsp;茂驴陆茂驴陆茂驴陆茂驴陆茂驴陆茂驴陆茂驴陆寐济柯矫柯矫柯矫柯矫柯矫柯矫柯?/font></a></td><td align=“right” class="" width=“40”><font color=“red”>03-14</font></td>

难道是 windows-1252编码不支持吗?我抓取utf-8的网页是正常的。。


6 回复

针对你遇到的 Node.js 爬虫抓取内容出现乱码问题,可能是由于目标网页使用了非 UTF-8 的字符集(如 windows-1252)。为了解决这个问题,我们需要确保爬虫正确识别并处理网页的字符集。

示例代码

首先,我们需要确保爬虫能够检测并处理不同字符集的内容。可以使用 iconv-lite 库来转换字符集。以下是一个改进后的示例代码:

const Crawler = require('crawler').Crawler;
const iconv = require('iconv-lite');

exports.index = function(req, res) {
    const c = new Crawler({
        maxConnections: 10,
        debug: true,
        forceUTF8: false,
        encoding: null, // 不自动解码,以便手动处理
        callback: function(error, result, $) {
            if (error) {
                console.error(error);
                return;
            }

            // 检测并转换字符集
            let decodedContent;
            try {
                decodedContent = iconv.decode(new Buffer(result.body), 'windows-1252');
            } catch (e) {
                console.error("Failed to decode with windows-1252:", e);
                decodedContent = result.body;
            }

            // 使用 jQuery 解析页面
            const te = $("#top .gengxin table tr:first").html();
            console.log(te);

            // 渲染页面
            res.render('index', { title: te });
        }
    });

    c.queue("http://psv.tgbus.com");
};

解释

  1. 设置 encoding: null

    • 我们将 encoding 设置为 null,这样爬虫不会自动解码响应体。这允许我们手动处理字符集。
  2. 手动解码

    • 使用 iconv-lite 库来解码响应体。这里假设目标网页使用的是 windows-1252 编码。如果目标网页使用其他编码,请相应地更改 iconv.decode 中的编码名称。
  3. 错误处理

    • 在解码过程中添加了异常处理,以防字符集不匹配导致的错误。

通过这种方式,我们可以确保爬虫能够正确处理不同字符集的网页内容,避免乱码问题。


爬虫要面对的第一个问题就是编码的问题。建议用fetch,自动转码。

并非不支持windows-1252编码,而是crawler调用的jschardet库将gb2312误判成windows-1252. 除非修改crawler的代码,无解。

自己抓,然后jquery dom

也可以爬虫用nodegrassex

出现乱码的原因通常是由于爬虫没有正确识别或处理网页的字符编码。在这个例子中,网页的编码被检测为 windows-1252,而默认情况下爬虫可能尝试使用 utf-8 进行解码,导致乱码。

为了正确处理字符编码,可以在爬虫配置中显式指定编码。以下是修改后的代码示例:

exports.index = function(req, res) {
    var Crawler = require("crawler").Crawler;

    var c = new Crawler({
        "maxConnections": 10,
        "debug": true,
        "encoding": "windows-1252",  // 显式指定字符编码
        "callback": function(error, result, $) {
            if (error) {
                console.error(error);
                return;
            }
            var te = $("#top .gengxin table tr:first").html();
            console.log(te);
            res.render('index', { title: te });
        }
    });

    c.queue("http://psv.tgbus.com");
};

在这个例子中,通过设置 encoding 选项为 "windows-1252",可以确保爬虫正确地处理页面中的字符编码,从而避免乱码问题。如果页面使用的是其他编码(例如 GBK),则需要相应地更改 encoding 的值。

回到顶部