为什么 phantomJS 无法从此页面获取标记?

Why is phantomJS unable to get markup from this page?

我已经在一些网站上尝试过此代码并且它有效但在其他网站上似乎 PhantomJS 和 casperJS 无法成功等待动态加载的内容。 JavaScript 加载的内容对于 casperJS 是不可见的,即使在等待几分钟后也是如此。

var casper = require('casper').create({
  pageSettings: {
    loadImages: false
  }
});


casper.start('https://betyetu.co.ke/sportsbook/SOCCER/');

casper.waitFor(function check() {
  return this.evaluate(function () {
    return document.querySelectorAll('div.events-app__group').length > 1;
  });
}, function then() {
  this.echo('Found elements');
  }, function timeout() {
    this.echo('Still timing out before returning element count');
  }, 60000);

casper.run();

当我只查询 div 而没有 class 时,它会 returns 显示匹配元素已找到的消息。这是来自不需要JS加载的外部脚手架。所有包含我感兴趣的数据的内部元素都不会加载/对 casperJS 不可用。为什么会这样?

CasperJS 是 运行 PhantomJS 脚本的辅助库。 PhantomJS 有相当过时的 Web 引擎,不支持现代 javascript,因此网站在 PhantomJS 中会越来越频繁地崩溃。目标站点是无法在 PhantomJS 甚至 Internet Explorer 11 中完全运行的站点之一。

但是使用 polyfills,为旧浏览器模拟新 js 功能的库,我们可以使用 CasperJS 更长时间。在这里,我 注入 优秀的 core.js 库,在 PhantomJS 中创建页面之后,但在访问该站点之前。这样我们的旧浏览器将拥有一组新的 javascript 功能。

var casper = require('casper').create({
    // it's better to blend with the crowd
    pageSettings: {
        userAgent: "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36",
        loadImages: false
    },
    viewportSize : { width: 1280, height: 720 },
    verbose: true,
});

// Apply the bandaid in the form of core.js polyfill
casper.on('page.initialized', function() {
    casper.page.injectJs('./core.js');
});

casper.start('https://betyetu.co.ke/sportsbook/SOCCER/');

casper.waitFor(function check() {
  return this.evaluate(function () {
    return document.querySelectorAll('div.events-app__group').length > 1;
  });
}, function then() {
  var count = this.evaluate(function () {
    return document.querySelectorAll('div.events-app__group').length;
  });
  this.echo('Found elements: ' + count);
  casper.capture('screen.jpg');
}, function timeout() {
    this.echo('Still timing out before returning element count');
}, 5000);

casper.run();

Found elements: 28