gpt4 book ai didi

javascript - 如何在casperjs中正确设置Interval?

转载 作者:行者123 更新时间:2023-12-03 03:24:55 25 4
gpt4 key购买 nike

我尝试使用网络服务器与外界对话,setInterval 希望它有时自动执行。

我的 casperjs 设置之一是 this.capture(x + 'apple.png');

如果 setInterval 运行三次,我的文件夹下会显示三个图像。

结果我只保存了一张图像:1apple.png

虽然我可以在我的终端上看到很多信息 enter image description here

我想问一下我应该错过哪一步?任何帮助,将不胜感激。提前致谢。

这是我今天的代码.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.pngenter image description here

最佳答案

实现您想要的效果的一种方法是使用 cron 作业以所需的频率抓取站点,并将结果放入由 Web 服务器提供服务的目录中。下面是一个独立的脚本,它将每小时获取一次网站的标题并捕获图像。修改 this.step 可能是不明智的(灵感来自此网站: https://github.com/yotsumoto/casperjs-goto )

var casper = require('casper').create();
var x = 0;

casper.start('http://localhost:8080', function() {
// change 'http://localhost:8080' to the site to be scraped
this.echo(this.getTitle());
this.capture(x++ + '.png');
this.wait(60 * 60 * 1000, function() {
// 60 minutes * 60 seconds * 1000 milliseconds
this.step = 0;
});
});

casper.run();

关于javascript - 如何在casperjs中正确设置Interval?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46376642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com