gpt4 book ai didi

javascript - PhantomJS 错误 : UnhandledPromiseRejectionWarning

转载 作者:行者123 更新时间:2023-11-30 11:31:01 26 4
gpt4 key购买 nike

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

我已经设法仅使用request 包来抓取数据,但是我要抓取的站点具有动态内容,而request 只能抓取此动态数据。

所以我做了一些研究,发现要实现这一点,并且基于 this SO question ,我需要通过 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();
})

关于javascript - PhantomJS 错误 : UnhandledPromiseRejectionWarning,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46221721/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com