PhantomJS does not return results

I am testing PhantomJS and trying to return all the startups listed on angel.co. I decided to go with PhantomJS, since I would need to paginate the pages on the first page by clicking "Next" at the bottom. Now this code does not return any results. I am completely new to PhantomJS and have read all the code examples, so any tutorial would be highly 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(); }); 
+4
source share
1 answer

By default, console messages rated on the page are not displayed in the PhantomJS console.

When you execute code under page.evaluate(...) , this code executes in the context of the page. Therefore, when you have console.log((i + 1) + ":" + list[i].innerText); , it is registered in the browser itself without a browser, and not in PhantomJS.

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

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

page.onConsoleMessage launched whenever you type on the console from inside the page. With this callback, you ask PhantomJS to echo the message to your standard output stream.

For reference, your final code will look (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(); }); 
+17
source

Source: https://habr.com/ru/post/1495858/


All Articles