【问题标题】:Wrong charset when scraping with Scraperjs使用 Scraperjs 抓取时出现错误的字符集
【发布时间】:2017-02-15 05:26:45
【问题描述】:

我目前正在使用 Scraperjs 抓取一个带有字符集 ISO-8859-1 的网站。我的问题是 æ、ø、å 和 é 等字符未正确编码(它们显示/保存为问号)。

有什么想法吗?

刮刀: https://github.com/ruipgil/scraperjs

【问题讨论】:

    标签: javascript node.js character-encoding web-scraping


    【解决方案1】:

    自己找到了解决方案。您需要将编码指定为二进制才能读取显示的字符。见以下代码:

            scraperjs.StaticScraper.create()
            .request({ url:"http://vg.no", encoding: "binary"})
            .scrape(function($) {
                return $("p").map(function() {
                    return $(this);
                });
            })
            .then(function(domElements).........
    

    【讨论】:

      【解决方案2】:
      const scraperjs = require('scraperjs');
      const urlToScrape = 'http://www.somesite.com';
      const selectorToScrape = "div#someId";
      scraperjs.StaticScraper.create({
          url: urlToScrape, 
          encoding: "binary"
      }).scrape(function ($) {
          return $(selectorToScrape).map(function () {
              return $(this).text();
          }).get();
      }).then(function (result) {
       console.log(result);
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-14
        相关资源
        最近更新 更多