PhantomJS Error: UnhandledPromiseRejectionWarning

PhantomJS Error: UnhandledPromiseRejectionWarning

我的目标是使用 Node.js 从网站上抓取一些数据。

我已经成功地使用 request 包抓取数据,但是我要抓取的站点有动态内容,而 request 只能抓取此动态数据。

所以我做了一些研究,发现要实现这一点,基于 ,我需要通过 npm 安装一些软件包(我不知道如果三个都需要):

同样基于问题,我使用了相同的代码,只是为了了解它是如何工作的:

myFile.js

var phantom = require('phantom');

phantom.create(function (ph) {
  ph.createPage(function (page) {
    var url = "http://www.bdtong.co.kr/index.php?c_category=C02";
    page.open(url, function() {
      page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
        page.evaluate(function() {
          $('.listMain > li').each(function () {
            console.log($(this).find('a').attr('href'));
          });
        }, function(){
          ph.exit()
        });
      });
    });
  });
});

但是当我在终端 $ node myFile.js 中尝试 运行 时,它不起作用并且一直给我错误:

(node:6576) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Unexpected type of parameters. Expecting args to be array.

(node:6576) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

有什么解决办法吗?

编辑:

最终解决方案基于@Shyam 的回答(解决了错误)和this example:

var phantom = require('phantom');
var _ph, _page, _outObj;

phantom
  .create()
  .then(ph => {
    _ph = ph;
    return _ph.createPage();
  })
  .then(page => {
    _page = page;
    return _page.open('https:/www.google.com.br/');
  })
  .then(status => {
    console.log(status);
    return _page.property('content');
  })
  .then(content => {
    console.log(content);
    _page.close();
    _ph.exit();
  })
  .catch(e => console.log(e))
;

我不确定你从哪里得到格式,但最新的 phantom JS 不使用回调,而是使用 promises。 constructor (Phantom.create) 需要数组形式的配置而不是回调函数。

我认为您的代码需要与此类似(我没有测试过但应该 运行)。

var phantom = require('phantom');
var _ph, _page;
phantom.create()
  .then(function (ph) {
    _ph = ph;
    return ph.createPage();
  })
  .then(function (page) {
    _page = page; 
    var url = "http://www.bdtong.co.kr/index.php?c_category=C02";
    return page.open(url);
  })
  .then(function(page) {
    page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
      page.evaluate(function() {
        $('.listMain > li').each(function () {
          console.log($(this).find('a').attr('href'));
        });
      });
    });
  })
  .catch(function(err) {
    _page.close();
    _ph.exit();
  })