問題描述
我有一個腳本可以打開鏈接數組中的 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.start
和 casper.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
.
這篇關于CasperJS/PhantomJS 分段錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!