PhantomJS非常慢,节点进程的CPU为100%

时间:2016-11-26 08:01:19

标签: node.js phantomjs

我正在搞乱phantomJS只是为了看看它是如何工作的,看起来它是超慢的并且在运行时固定CPU。我觉得我一定做错了。我觉得我的代码不应该固定CPU,只需要几秒钟就可以运行,但最终耗时超过20秒。我的代码和输出如下。

var phantom = require('phantom');

const startTime = new Date().getTime();
let page = null;

function gimmeTime() {
  return new Date().getTime();
}

phantom.create(['--ignore-ssl-errors=yes', '--load-images=no'], {
  phantomPath: './node_modules/phantomjs-prebuilt/bin/phantomjs'
})
  .then(instance => {
    phInstance = instance;
    return instance.createPage();
  })
  .then(p => {
    console.log(gimmeTime() - startTime);
    page = p;

    return page.open("http://www.slickdeals.com/");
  })
  .then(status => {
    console.log(gimmeTime() - startTime);
    return page.includeJs('https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js');
  })
  .then(status => {
    console.log(gimmeTime() - startTime);
    return page.evaluate(function() {
      return href = 'http://slickdeals.com' + $('.fpGridBox:first .itemImageLink').attr('href');
    });
  })
  .then(href => {
    console.log(gimmeTime() - startTime);
    console.log('href is', href);
    return page.open(href);
  })
  .then(status => {
    console.log(gimmeTime() - startTime);
    return page.includeJs('https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js');
  })
  .then(status => {
    console.log(gimmeTime() - startTime);
    return page.evaluate(function() {
      return $('title').text();
    });
  })
  .then(author => {
    console.log(author);
    console.log(gimmeTime() - startTime);
    phInstance.exit();
    return false;
  })
  .catch(error => {
      console.log(error);
      phInstance.exit();
  });

输出:

436
6617
15921
15955
href is http://slickdeals.com/f/9159375-10-lb-musclepharm-combat-protein-powder-cookies-n-cream-60?src=featured-12075
15965
22150
Slickdeals: The Best Deals, Coupons, Promo Codes & Discounts
22163

运行: 苹果系统 节点:7.2 Phantomjs:2.1.1 幻影:3.1

0 个答案:

没有答案