0
我嘗試使用webserver與外界通信,並且setInterval每次都要自動執行。如何在casperjs中設置正確的間隔?
我的一個casperjs設置爲this.capture(x + 'apple.png');
不過,我覺得它會如果setInterval
跑三圈顯示我的文件夾下的三幅圖像。
因此,我只保存一個圖像是1apple.png
。
我想問哪一步應該怎麼錯過呢?任何幫助,將不勝感激。 在此先感謝。
這裏是我的代碼today.js:
var webserver = require('webserver');
var server = webserver.create();
var service = server.listen('8080', {
'keepAlive': true
}, function (request, response) {
response.statusCode = 200;
response.write('<html><body>What the hell~~</body></html>');
var casper = require("casper").create({
verbose: true,
logLevel: 'debug', // debug, info, warning, error
pageSettings: {
loadImages: false,
loadPlugins: false,
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'
}
});
var movieTitle = [];
var movieEnTitle = [];
var title = [];
var movieTime = [];
var movieVersion = [];
var city = '臺南';
var latitude = 22.993089;
var longitude = 120.196876;
var theaterName = '今日戲院';
var data = {};
data.theater = [];
data.movie = [];
var x =1;
function getMovieTitle() {
var title = document.querySelectorAll('div.theaterlist_name a');
return Array.prototype.map.call(title, function (e) {
return e.innerText;
});
};
function getEnTitle() {
var title = document.querySelectorAll('div.en a');
return Array.prototype.map.call(title, function (e) {
return e.innerText;
});
};
function getMovieTime() {
var title = document.querySelectorAll('ul.theater_time');
return Array.prototype.map.call(title, function (e) {
return e.innerText;
});
};
function getMovieVersion() {
var version = document.querySelectorAll('div.tapR');
return Array.prototype.map.call(version, function (e) {
return e.innerText;
});
};
// 臺南 今日戲院 from 奇摩
casper.start('https://tw.movies.yahoo.com/theater_result.html/id=67', function() {
this.echo(this.getTitle());
});
casper.then(function() {
this.echo('Image');
this.echo(x);
this.capture(x + 'apple.png');
x++;
});
casper.then(function() {
movieTitle = this.evaluate(getMovieTitle);
movieEnTitle = this.evaluate(getEnTitle);
movieTime = this.evaluate(getMovieTime);
movieVersion = this.evaluate(getMovieVersion);
});
casper.then(function() {
console.log('Print:\n');
this.echo(movieTitle.length + ' Movie title found :\n');
this.echo(movieTitle.join('\n'));
this.echo(movieEnTitle.length + ' Movie title found :\n');
this.echo(movieEnTitle.join('\n'));
this.echo(movieTime.length + ' Movie time found :\n');
this.echo(movieTime.join('\n'));
this.echo(movieVersion.length + ' Movie version found :\n');
this.echo(movieVersion.join('\n'));
this.echo(outPutJSON());
});
function outPutJSON() {
data.theater.push({
name: theaterName,
city: city,
latitude: latitude,
longitude: longitude
});
// 將中英文名字合併
for (var i = 0; i < movieTitle.length; i++) {
title.push(movieTitle[i] + movieEnTitle[i]);
}
for (var i = 0; i < movieTime.length; i++) {
var name = title[i];
var sourceTime = movieTime[i].match(/.{1,5}/g);
var times = [];
times.push(sourceTime);
var version = movieVersion[i];
data.movie.push({
name: name,
version: version,
time: times
});
}
return JSON.stringify(data);
}
// casper.run(function() {
// // this.echo('Done').exit();
// this.echo('Done');
// });
setInterval(function() {
casper.run(function() {
// this.echo('Done').exit();
this.echo('Done');
});
}, 2000);
response.write(outPutJSON());
response.close();
});
這裏是我的文件夾時,我指揮這個文件,你可以看到一次1apple.png
只捕獲圖像。
感謝您的回覆,我更新的問題,以顯示我想要什麼。我在casper.start之後設置了一些函數,我的問題是爲什麼setInterval不能再次運行我的this.capture? –
感謝您的更新,但我仍然不確定自己明白你的代碼試圖做什麼。您是否試圖抓取網頁並每隔2秒捕獲一次?繞過casperjs調用的Web服務器的目的是什麼? – davejagoda
好吧,2秒只是一個測試,真正我想要的可能是一兩個小時。函數this.capture(x +'apple.png');是在casper.start之後。當我與網絡服務器通話時,我會每小時收到一個png。像1apple.png,2apple.png,3apple.png像這樣...但在我的代碼中,我只得到1apple.png。它就像我的項目文件夾。 –