示例Casper JS Script不起作用

时间:2013-11-26 17:55:24

标签: javascript web-scraping casperjs

更新:我找到了解决方案而忘记添加

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

在顶部。

原始问题: 我安装了幻影js 1.9.2和casper js 1.0.3。另外,我直接从casper js教程页面复制并粘贴了以下示例脚本:

casper.start('http://www.google.fr/', function() {
    var url = 'http://www.google.fr/intl/fr/about/corporate/company/';
    this.download(url, 'google_company.html');
});

casper.run(function() {
    this.echo('Done.').exit();
});

这是我的mac os lion上的输出:

bin Tom$ ./casperjs ../site_loader.js 
2013-11-26 18:53:20.375 phantomjs[2136:130b] *** WARNING: Method userSpaceScaleFactor in class NSView is deprecated on 10.7 and later. It should not be used in new applications. Use convertRectToBacking: instead. 
ReferenceError: Can't find variable: casper

  ../site_loader.js:1
Hint: you may want to use the `casperjs test` command.
^C

我必须手动取消它,否则它永远不会停止。但是,运行建议的命令会产生:

bin Tom$ ./casperjs test
2013-11-26 18:54:01.504 phantomjs[2137:130b] *** WARNING: Method userSpaceScaleFactor in class NSView is deprecated on 10.7 and later. It should not be used in new applications. Use convertRectToBacking: instead. 
No test path passed, exiting.

这里有什么不正确的?

1 个答案:

答案 0 :(得分:4)

首先,您需要创建一个casper实例才能使用,然后您编写的所有内容都将有效!

// sample.js
var casper = require('casper').create();

casper.start('http://www.google.fr/', function() {
    var url = 'http://www.google.fr/intl/fr/about/corporate/company/';
    this.download(url, 'google_company.html');
});

casper.run(function() {
    this.echo('Done.').exit();
});

输出:

C:\Users\hello\Desktop>caperjs sample.js
Done.

C:\Users\hello\Desktop>ls -alt
-rwx------ hello mkpasswd 91 Nov 28 09:11 google_company.html 
...