PhantomJS似乎不止一次加载页面

PhantomJS seems to be loading pages more than once

本文关键字:加载 不止一次 PhantomJS      更新时间:2023-09-26

我正在尝试使用PhantomJS进行页面/导航自动化实验。然而,当试图打开URL并导航到其他页面时,我发现页面加载不止一次。

以下是我尝试搜索术语并使用下一步按钮导航到其他页面的场景。然而,正如日志中显示的那样,后续页面被打开了不止一次,并且它也会以指数方式增长,也会出现一些空白回调。

我做错了什么吗?

function pageRequest(url){
    function callback(){
        console.log("pass  ----------------- " );
        page.includeJs("//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js", function() {
            var x = page.evaluate(function() {
                    var ret = null;
                  $("a").each(function(ele, i){
                    // console.log($(i).attr("href"));
                    if($(i).text().toLowerCase().indexOf("next")  != -1){
                        // console.log("Hurray !!");
                        ret  = $(i).attr("href");
                        return $(i).attr("href");
                    }
                  });
                  return ret;
            });
            console.log(x);
            // return;
            if(x){
                page.open("http://torrentz.in" + x, callback);
            }
        });
    }
    var page = require('webpage').create();
    page.onConsoleMessage = function(msg) {
      console.log('Page title is ' + msg);
    };
    page.open(url, callback);
}
pageRequest("http://torrentz.in/search?f=test");
//LOGS
$ phantomjs capture.js 
pass  ----------------- 
/search?f=test&p=1
pass  ----------------- 
/search?f=test&p=2
/search?f=test&p=2
pass  ----------------- 
/search?f=test&p=2
/search?f=test&p=2
/search?f=test&p=2
pass  ----------------- 
pass  ----------------- 
pass  ----------------- 
pass  ----------------- 
/search?f=test&p=3
/search?f=test&p=3
/search?f=test&p=3
/search?f=test&p=3
/search?f=test&p=3
/search?f=test&p=3
/search?f=test&p=3
pass  ----------------- 
pass  ----------------- 
pass  ----------------- 
pass  ----------------- 
pass  ----------------- 
pass  ----------------- 
pass  ----------------- 
/search?f=test&p=4
/search?f=test&p=4
/search?f=test&p=4
/search?f=test&p=4
/search?f=test&p=4
/search?f=test&p=4
/search?f=test&p=4
/search?f=test&p=4
/search?f=test&p=4
/search?f=test&p=4
/search?f=test&p=4
/search?f=test&p=4
/search?f=test&p=4
/search?f=test&p=4
pass  ----------------- 
pass  ----------------- 
pass  ----------------- 
pass  ----------------- 
pass  ----------------- 
^C

你可以尝试创建一个新的页面,像这样:

if(x){
  page = require('webpage').create();
  page.open("http://torrentz.in" + x, callback);
}