我试图在 PhantomJS 之上构建一个非常简单的解决方案,但不知何故在调用 phantom.exit()
时它崩溃了。例如,运行以下代码时:
var page = require('webpage').create();
page.onResourceRequested = function (request) {
console.log('Request ' + request.url);
phantom.exit();
};
page.open('http://www.google.com/');
在 Ubuntu (12.04.3 LTS) 和 CentOS (6.4) 上因段错误而崩溃。如果我注释掉 phantom.exit()
它会列出请求的 URL,因此它基本上按预期工作。然而 phantom.exit()
是我真正需要的东西,因为我想在某些情况下终止我的脚本(例如,尝试访问特定域)。
我觉得我的问题太明显了,但不知道发生了什么。
最佳答案
是a known issue ,但有一个简单的解决方法。不过,这是一个应该修复的错误,但现在:
setTimeout(function()
{
phantom.exit(0);
},0);
这很愚蠢,但应该可以解决问题...我链接到的 github 页面上也有一个补丁。还有一些关于为什么存在此错误的更多详细信息。
我认为问题出在这里:
Yes, you're right [phantom.exit should terminate PhantomJS without executing any further code.], but each call to m_pages[i]->close(); calls deleteLater() this means that each object will be deleted, but asynchronous... maybe this is the problem.
异步删除在我看来是导致错误的可能原因,因为处理程序在 setTimeout
之后返回,而实际的 exit
调用被延迟,一切都可以何时何地删除。超时有效地将 exit
调用移出处理程序的范围...
我还没有测试过,但如果是这样的话,也许:
var closeFunc = function()
{
phantom.exit(0);
};
page.onResourceRequested = function (request)
{
console.log('Request ' + request.url);
return closeFunc();
};
也可以。如果确实如此,那么您将不得不使用这些解决方法中的任何一种,直到发布可靠的补丁或下一个版本并修复错误。
关于javascript - PhantomJS 在 Linux 上的 phantom.exit() 后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19144632/