简体   繁体   English

如何在casperjs中正确设置setInterval?

[英]How to setInterval correct in casperjs?

I try to use webserver talk to outside world and setInterval want its execution automatically every sometimes. 我尝试使用网络服务器与外界对话,而setInterval有时希望每次自动执行它。

One of my casperjs setting is this.capture(x + 'apple.png'); 我的casperjs设置之一是this.capture(x + 'apple.png');

I though it will show three images under my folder if setInterval run three times. 尽管如果setInterval运行3次,它将在文件夹下显示3张图像。

As the result i only save one image is 1apple.png . 结果,我只保存一张图像是1apple.png

Although i can see a lots of info on my terminal 虽然我可以在终端上看到很多信息 在此处输入图片说明

I want to ask what step should i miss it ? 我想问我应该错过哪一步? Any help would be appreciated. 任何帮助,将不胜感激。 Thanks in advance. 提前致谢。

Here is my code today.js: 这是我的代码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();
});

Here is my folder when i command this file , you can see only capture image once 1apple.png . 当我命令该文件时,这是我的文件夹,一旦1apple.png ,您只能看到捕获图像。 在此处输入图片说明

One way to achieve what you want would be have a cron job scrape the site at the desired frequency and put the results in a directory served by a web server. 一种实现所需目标的方法是,通过cron作业以所需的频率抓取站点并将结果放入Web服务器提供的目录中。 Below is a stand-alone script that will fetch the title and capture the image of a site once per hour. 下面是一个独立的脚本,它将每小时提取一次标题并捕获站点的图像。 Modifying this.step is probably unwise (inspiration from this site: https://github.com/yotsumoto/casperjs-goto ) 修改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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM