简体   繁体   English

在PhantomJS中循环创建页面对象

[英]Creating page objects in a loop in PhantomJS

I'm using PhantomJS to retrieve the HTML of many distinct URLS. 我正在使用PhantomJS检索许多不同URL的HTML。 To do this, I'm iterating over an array of URLs and trying to create PhantomJS page objects (documentation here ). 为此,我要遍历URL数组并尝试创建PhantomJS页面对象( 此处的文档)。

For some reason, the console.log inside the page.open block isn't firing. 由于某些原因, page.open块内的console.log无法触发。 Does anyone have any idea why? 有谁知道为什么吗? I can't find a similar problem on Google or Stack Overflow or PhantomJS documentation. 我在Google或Stack Overflow或PhantomJS文档中找不到类似的问题。

Code: 码:

for(var i = 0; i < urlList.length; i++) {
    urlToRequest = urlList[i];
    var page = require('webpage').create();
    page.open(urlToRequest, function (status) {
        console.log("Status code: ", status);
        phantom.exit()
    });
}

By default console.log output from the page is suppressed. 默认情况下,该页面的console.log输出被禁止。 To print it out define an onConsoleMessage handler for the page object like is done below and it will print. 要打印出来,请为页面对象定义一个onConsoleMessage处理程序,如下所示,它将进行打印。

page.onConsoleMessage = function (msg, lineNum, sourceId) {
     console.log(msg);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM