CasperJS/PhantomJS 分段错误

我有一个脚本可以打开链接数组中的 url,并且对于每个 url,它会提取该 url 中的链接并将新链接插入到数组链接(addLinks 函数).该代码导致分段错误,并且当它调用 this.start 打开一个有效的 url(代码的第 3 行)时会发生这种情况.是 casperjs 还是我的代码有问题?

另一个有趣的点是它总是在页面标题:"之后打印OK,它已加载",而根据代码,它们应该以相反的顺序打印.你能告诉我这种奇怪行为的原因吗?

//只是打开页面并打印标题功能开始(链接){this.echo('让我们试试链接:-- '+ 链接 + ' -------------');this.start(链接,函数(){this.echo('页面标题:' + this.getTitle());});this.echo('OK,加载完毕
');}功能检查(){if (links[currentLink] && currentLink < upTo) {this.echo('--- 链接 ' + currentLink + ' ---');start.call(this, links[currentLink]);addLinks.call(this, links[currentLink]);当前链接++;this.run(检查);} 别的 {this.echo("全部完成.");this.exit();}}casper.start().then(function() {this.echo("开始");});casper.run(检查);

这是我的代码的结果:

<上一页>--- 链接 0 ---让我们试试链接:-- http://yahoo.com -------------好的,已加载页面标题:雅虎找到 111 个链接 http://yahoo.com找到 13 个脚本 http://yahoo.com找到 0 帧 http://yahoo.com帧源:新框架源:--- 链接 1 ---让我们试试链接:-- http://everything.yahoo.com/-------------好的,已加载PhantomJS 崩溃了.请阅读崩溃报告指南...分段错误(核心转储)

解决方案

casper.startcasper.run 只在你的 once 中使用脚本.您可以将 this.start 重命名为 this.thenOpen 并将 this.run 重命名为 this.then.

I have a script which opens the urls in the links array and for each url it extracts the links in that url and inserts the new links to the array links(addLinks function). The code results in segmentation fault and it happens when it calls this.start to open a valid url (3rd line of the code). Is it a problem with casperjs or my code?

Another interesting point is that it always prints 'OK, it is loaded' after the 'Page title: ' while according to the code they should be printed in reverse order. Would you please tell me the reason for this strange behaviour?

// Just opens the page and prints the title
function start(link) {
    this.echo('lets try the link:--  '+ link + ' -------------');
    this.start(link, function() {
        this.echo('Page title: ' + this.getTitle());
    });
    this.echo('OK, it is loaded
');
}

function check() {
    if (links[currentLink] && currentLink < upTo) {
        this.echo('--- Link ' + currentLink + ' ---');
        start.call(this, links[currentLink]);
        addLinks.call(this, links[currentLink]);
        currentLink++;
        this.run(check);
    } else {
        this.echo("All done.");
        this.exit();
    }
}
casper.start().then(function() {
    this.echo("Starting");
});

casper.run(check);

Here is the result of my code:

--- Link 0 ---
lets try the link:--  http://yahoo.com -------------
OK, it is loaded

Page title: Yahoo
111 links found http://yahoo.com
13 scripts found http://yahoo.com
0 frames found http://yahoo.com
 frame src:
new frame src:
--- Link 1 ---
lets try the link:--  http://everything.yahoo.com/ -------------
OK, it is loaded

PhantomJS has crashed. Please read the crash reporting guide...
Segmentation fault (core dumped)

解决方案

Use casper.start and casper.run only once in your script. You can rename this.start to this.thenOpen and this.run to this.then.

相关文章