javascript - Serialize function with PhantomJS bridge -
i have array of links use link parametr function scraped data phantomjs. how serilize function? for statemant runs paralely 3 function in 1 time , recive event error.
in case proper use async, how use in series? time of running functions different, how async should understood it's done , start new url?
var phantom = require('phantom') , async = require('async'); var urls = [ 'http://en.wikipedia.org/wiki/main_page', 'http://es.wikipedia.org/wiki/wikipedia:portada', 'http://de.wikipedia.org/wiki/wikipedia:hauptseite' ]; async.mapseries(urls, gettitle, function(err, result){ console.log(result); }) function gettitle (link, callback) { phantom.create(function(ph) { return ph.createpage(function(page) { return page.open(link, function(status) { return page.evaluate((function() { return document.title; }), function(result) { callback(null, result); return ph.exit(); }); }); }); }); };
i'd try like:
var links = [] var _ph function init(cb) { phantom.create(function(ph) { //for each link in links call dostuff() _ph = ph dostuff(ph, link, cb) }) } function dostuff(ph, link, cb) { ph.createpage(function(page) { //does things in parallel? page.open(link, function(status) { page.evaluate((function() { document.title; }), function(result) { cb(null, result); page.close(); }); }); } var counter = links.length var titles; function results(err, res) { titles.push(res) if(--counter == 0) { //done _ph.exit() } } init(results)
probably not working code (i wrote here), hope idea. if want use 1 page, like:
var links = [] var _ph var _page function init(cb) { phantom.create(function(ph) { _ph = ph ph.createpage(function(page) { _page = page dostuff(link, cb) } }) } function dostuff(page, link, cb) { page.open(link, function(status) { page.evaluate((function() { document.title; }), function(result) { cb(null, result); page.close(); }); }); } var counter = links.length var titles; function results(err, res) { titles.push(res) if(--counter == 0) { //done _ph.exit() return } dostuff(links[counter], results) } init(results)
Comments
Post a Comment