node爬取网站

发布于 2022-09-12 01:48:56 字数 1194 浏览 23 评论 0

大概几百个网站,需要网站的名称,使用的是node-crawler这个插件,这些网站的字符集编码有些不一样,有的网站是utf-8的,有的是gb2312的,还有其他格式的字符集,怎么才能自动识别字符集编码?

var defaultOptions = {
    autoWindowClose: true,
    forceUTF8: true,
    gzip: true,
    incomingEncoding: null,
    jQuery: true,//res 是否注入 cheerio,doc有详细说明
    maxConnections: 10,//只有在rateLimit == 0时起作用,限制并发数
    method: 'GET',
    priority: 5,//queue请求优先级,模拟用户行为
    priorityRange: 10,
    rateLimit: 0,//请求最小间隔
    referer: false,
    retries: 3,//重试次数,请求不成功会重试3次
    retryTimeout: 10000,//重试间隔
    timeout: 15000,//15s req无响应,req失败
    skipDuplicates: false,//url去重,建议框架外单读使用seenreq
    rotateUA: false,//数组多组UA
    homogeneous: false
};

var c = new Crawler({

    forceUTF8: true,
    maxConnections: 10,
    // This will be called for each crawled page
    callback: function (error, res, done) {
        if (error) {
            console.log(error);
        } else {
            var $ = res.$;
            console.log(res)
            // $ is Cheerio by default
            //a lean implementation of core jQuery designed specifically for the server
            // console.log($("title").text());
        }
        done();
    }
});
c.queue(arr[0]);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梅倚清风 2022-09-19 01:48:56

抓取之前,先异步请求下首页内容,从响应头或响应内容中取得编码即可

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文