CasperJS同时请求 [英] CasperJS simultaneous requests
问题描述
让我们说我有一个网址数组。我不想使用thenOpen函数。因为它等待每个先前的url加载,并且减少了加载时间。
casper.each(hrefs,function(self, href){
self.thenOpen(href,function(){});
self.then(function(){
//选择器
});
});
您将使用什么方法消费与上述方法相比要少得多吗?在db中创建多个实例存储然后取回效率很高吗...但这令人头疼。并且还希望您也回答一般情况下,当我同时运行同一js文件的多个实例时会遇到问题吗?
如果您不关心要在所有打开的URL之间进行同步的行为,则应该为每个URL启动Casper的多个实例。下面是一个示例:
var casperActions = {
href1:function(casper){
casper。 start(address,function(){...});
//测试,而不用于href1
casper.run(function(){...});
},
href2:函数(casper){
casper.start(address,function(){...});
//测试和href2
不适用的内容casper.run(function(){...});
},
...
};
['href1','href2',...]。each(function(href){
var casper1 = require('casper')。create();
casperActions [href](casper);
});
每个实例都可以独立运行,但可以同时访问多个URL。 / p>
Lets say I have an array of urls. I dont want to use thenOpen function . Since it waits for every previous url to be loaded and it decreases load time .
casper.each(hrefs,function(self,href){
self.thenOpen(href,function(){ });
self.then(function(){
// Selectors
});
});
What methods would u use to spend much less compared to above method ? Would it be efficient to create multiple instances store in the db and then fetch ... but this is alot of headache . And also would like u also to answer in general would I have problems when I run multiple instances of the same js file simultaneously ?
If you don't care about synchronizing behavior between all the URLs that you are opening, then you should start multiple instances of casper for each URL. Here is an example:
var casperActions = {
href1: function (casper) {
casper.start(address, function() {...});
// tests and what not for href1
casper.run(function() {...});
},
href2: function (casper) {
casper.start(address, function() {...});
// tests and what not for href2
casper.run(function() {...});
},
...
};
['href1', 'href2', ...].each(function(href) {
var casper1 = require('casper').create();
casperActions[href](casper);
});
Each instance would run independently of each other, but it would allow you to hit many URLs simultaneously.
这篇关于CasperJS同时请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!