如何设置Phantomjs页面抓取的时间间隔

目前我写了一个Phantomjs的脚本,通过多个页面。 我的脚本工作,但我不知道如何设置时间间隔擦伤。 我尝试使用setInterval并传递大约每5秒从arrayList的项目,但它似乎并没有工作。 我的脚本不断打破。 这是我的示例phantomjs脚本代码:

没有setInterval

 var arrayList = ['string1', 'string2', 'string3'....] arrayList.forEach(function(eachItem) { var webAddress = "http://www.example.com/eachItem" phantom.create(function(ph) { return ph.createPage(function(page) { return page.open(yelpAddress, function(status) { console.log("opened site? ", status); page.injectJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', function() { setTimeout(function() { return page.evaluate(function() { //code here for gathering data }, function(result) { return result ph.exit(); }); }, 5000); }); }); }); }); 

setInterval

 var arrayList = ['string1', 'string2', 'string3'....] var i = 0 var scrapeInterval = setInterval(function() { var webAddress = "http://www.example.com/arrayList[i]" phantom.create(function(ph) { return ph.createPage(function(page) { return page.open(yelpAddress, function(status) { console.log("opened site? ", status); page.injectJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', function() { setTimeout(function() { return page.evaluate(function() { //code here for gathering data }, function(result) { return result ph.exit(); }); }, 5000); }); }); }); i++ if(i > arrayList.length) { clearInterval(scrapeInterval); }, 5000); 

基本上,我想在arrayList中发送一大堆itemss(10-20个),等待1-2分钟,然后发送下一个大块的项目,而不会压倒网站。 或者如果有一种方法来设置一个时间间隔,每隔2-3秒循环遍历数组中的每个项目。

问题是PhantomJS是asynchronous的,但循环迭代不是。 所有的迭代(在第一个片段中)甚至在第一页被加载之前执行。 你本质上是生成多个同时运行的进程。

你可以使用像async这样的东西让它顺序运行:

 phantom.create(function(ph) { ph.createPage(function(page) { var arrayList = ['string1', 'string2', 'string3'....]; var tasks = arrayList.map(function(eachItem) { return function(callback){ var webAddress = "http://www.example.com/" + eachItem; page.open(webAddress, function(status) { console.log("opened site? ", status); page.injectJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', function() { setTimeout(function() { return page.evaluate(function() { //code here for gathering data }, function(result) { callback(null, result); }); }, 5000); }); }); }; }); async.series(tasks, function(err, results){ console.log("Finished"); ph.exit(); }); }); }); 

当然,你也可以在每个任务中移动phantom.create() ,这将为每个请求创build一个单独的进程,但是上面的代码会更快。

您在第二个片段中添加了setInterval方法:

 var arrayList = ['string1', 'string2', 'string3']; var i = 0; var scrapeInterval = setInterval(function () { var webAddress = "http://www.example.com/arrayList[i]" phantom.create(function (ph) { return ph.createPage(function (page) { return page.open(yelpAddress, function (status) { console.log("opened site? ", status); page.injectJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', function () { setTimeout(function () { return page.evaluate(function () { //code here for gathering data }, function (result) { return result ph.exit(); }); }, 5000); }); }); }); i++; if (i > arrayList.length) { clearInterval(scrapeInterval); } //This was missing; }); //This was missing; }, 5000); 

而且我注意到,是在下面的超时return语句:

 setTimeout(function () { return page.evaluate(function () { //code here for gathering data }, function (result) { return result ph.exit(); }); }, 5000); 

ph.exit(); 将永远不会达成,我不知道这是否会给你造成任何问题,但你可能想看看它。