Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

爬虫方案之PhantomJS #10

Open
xizhibei opened this issue May 6, 2016 · 0 comments
Open

爬虫方案之PhantomJS #10

xizhibei opened this issue May 6, 2016 · 0 comments
Labels

Comments

@xizhibei
Copy link
Owner

xizhibei commented May 6, 2016

发现zombie.js还是太弱了,很多地方不能满足需求,包括request的HTTP头也不合乎标准,于是硬着头去尝试下PhantomJS。

这一试,我就后悔了,这么好的东西我居然不先尝试!怎么说呢,它比zombie好用太多,加载处理速度方面也是远远超过。唯一比较遗憾的是没有原生的nodejs接口,它只是一个环境,像mocha这样有自定义的变量,需要用它去执行js文件。所以如果要用到爬虫上面的话,需要通过第三方的包去包装一层。比如我用的https://github.com/amir20/phantomjs-node

目前我没找到这个库一些常用的方法utils,比如waitFor之类的没有实现,所以只能手动实现了,下面贴出我自己实现的:

async function waitFor(testFx, {
  timeout = 3000,
  delay = 100,
  silence = false,
}) {
  function sleep(t) {
    return new Promise((resolve) => {
      setTimeout(resolve, t);
    });
  }

  const start = new Date().getTime();

  async function _waitFor() {
    if (new Date().getTime() - start > timeout) {
      const e = new Error('Timeout');
      if (silence) return e;
      throw e;
    }
    const result = await testFx();
    if (!result) {
      await sleep(delay);
      return await _waitFor();
    }
  }

  return await _waitFor();
}

当然了,其他成熟的框架也有,比如

SlimerJS

跟PhantomJS几乎一样,API接口一致,但是没有PhantomJS不能获取response body的问题,是个替代品,但是它是基于Gecko的

CasperJS

包装了PhantomJS以及SlimerJS,提供了一些语法糖,高层实现,挺适合做e2e测试的

SpookyJS

基于CasperJS,提供了nodes的API,就相当于上面phantomjs-node对PhantomJS封装,如果下次再做的话,我会考虑这个包。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant