PhantomJS不返回结果 [英] PhantomJS not returning results

查看:127
本文介绍了PhantomJS不返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试PhantomJS,并试图退还angel.co上列出的所有创业公司.我决定选择PhantomJS,因为我需要通过单击底部的下一步"来对首页进行分页.目前,此代码不返回任何结果.我是PhantomJS的新手,已经阅读了所有代码示例,因此任何指导将不胜感激.

I'm testing out PhantomJS and trying to return all startups listed on angel.co. I decided to go with PhantomJS since I would need to paginate through the front page by clicking "Next" at the bottom. Right now this code does not return any results. I'm completely new to PhantomJS and have read through all the code examples so any guidance would be much appreciated.

var page = require('webpage').create();
page.open('https://angel.co/startups', function(status) {
if (status !== 'success') {
    console.log('Unable to access network');
} else {
     page.evaluate(function() {
        var list = document.querySelectorAll('div.resume');
        for (var i = 0; i < list.length; i++){
           console.log((i + 1) + ":" + list[i].innerText);
        }
     });
}
phantom.exit();
});

推荐答案

默认情况下,在页面上评估的控制台消息不会显示在您的PhantomJS控制台中.

By default, console messages evaluated on the page will not appear in your PhantomJS console.

page.evaluate(...)下执行代码时,该代码正在页面的上下文中执行.因此,当您拥有console.log((i + 1) + ":" + list[i].innerText);时,该内容将被记录在无头浏览器本身中,而不是在PhantomJS中记录.

When you execute code under page.evaluate(...), that code is being executed in the context of the page. So when you have console.log((i + 1) + ":" + list[i].innerText);, that is being logged in the headless browser itself, rather than in PhantomJS.

如果您希望将所有控制台消息都传递给PhantomJS本身,请在打开页面后使用以下内容:

If you want all console messages to be passed along to PhantomJS itself, use the following after opening your page:

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

每当您从页面内打印到控制台时,都会触发

page.onConsoleMessage.通过此回调,您要求PhantomJS将消息回显到其自己的标准输出流.

page.onConsoleMessage is triggered whenever you print to the console from within the page. With this callback, you're asking PhantomJS to echo the message to its own standard output stream.

作为参考,您的最终代码应如下所示(对我而言,此打印成功):

For reference, your final code would look like (this prints successfully for me):

var page = require('webpage').create();

page.open('https://angel.co/startups', function(status) {
    page.onConsoleMessage = function (msg) { console.log(msg); };
    if (status !== 'success') {
        console.log('Unable to access network');
    } else {
         page.evaluate(function() {
            var list = document.querySelectorAll('div.resume');
            for (var i = 0; i < list.length; i++){
               console.log((i + 1) + ":" + list[i].innerText);
            }
         });
    }
    phantom.exit();
});

这篇关于PhantomJS不返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆