PhantomJS not returning results

孤街浪徒 提交于 2019-12-19 03:53:23

问题


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();
});

回答1:


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

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.

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 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();
});


来源:https://stackoverflow.com/questions/18115888/phantomjs-not-returning-results

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!