如何在node.js中使用CasperJS?

ati*_*n25 34 node.js phantomjs

我想在node.js中使用CasperJS.

我已经引用了以下URL来在node.js中使用CasperJS:

在上述URL的帮助下,我编写了以下代码:

//DISPLAY=:0 node test2.js
var phantom = require('phantom');
console.log('Hello, world!');
phantom.create(function (ph) {
    ph.casperPath = '/opt/libs/casperjs'
    ph.injectJs('/opt/libs/casperjs/bin/bootstrap.js');
    var casper = require('casper').create();
    casper.start('http://google.fr/');

    casper.thenEvaluate(function (term) {
        document.querySelector('input[name="q"]').setAttribute('value', term);
        document.querySelector('form[name="f"]').submit();
    }, {
        term: 'CasperJS'
    });

    casper.then(function () {
        // Click on 1st result link
        this.click('h3.r a');
    });

    casper.then(function () {
        console.log('clicked ok, new location is ' + this.getCurrentUrl());
    });

    casper.run();
});
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,我收到以下错误:

错误味精:

tz@tz-ubuntu:/opt/workspaces/TestPhantomjs$ DISPLAY=:0 node test2.js 
Hello, world!
Error: Cannot find module 'casper'
    at Function._resolveFilename (module.js:332:11)
    at Function._load (module.js:279:25)
    at Module.require (module.js:354:17)
    at require (module.js:370:17)
    at /opt/workspaces/TestPhantomjs/test2.js:6:14
    at Object.<anonymous> (/opt/workspaces/TestPhantomjs/node_modules/phantom/phantom.js:82:43)
    at EventEmitter.<anonymous> (/opt/workspaces/TestPhantomjs/node_modules/phantom/node_modules/dnode/index.js:215:30)
    at EventEmitter.emit (events.js:67:17)
    at handleMethods (/opt/workspaces/TestPhantomjs/node_modules/phantom/node_modules/dnode-protocol/index.js:138:14)
    at EventEmitter.handle (/opt/workspaces/TestPhantomjs/node_modules/phantom/node_modules/dnode-protocol/index.js:98:13)
phantom stdout: Unable to load casper environment: Error: Failed to resolve module fs, tried fs
Run Code Online (Sandbox Code Playgroud)

NiK*_*iKo 25

您可以使用SpookyJS从节点驱动CasperJS.

  • 是的..我无法让他们的[你好世界](https://github.com/WaterfallEngineering/SpookyJS/issues/39)工作! (7认同)
  • 该模块中有很多错误.它甚至没有从npm安装干净. (2认同)

ati*_*n25 21

https://groups.google.com/group/casperjs/browse_thread/thread/641e9e6dff50fb0a/e67aaef5ab4ec918?hl=zh-CN#e67aaef5ab4ec918

Nicolas Perriault
2012/2/27天猪蓝虫.:

我想在nodejs中使用casperjs.并参考:https: //github.com/sgentle/phantomjs-nodehttp://casperjs.org/index.html#faq-executable

你不能这样运行CasperJS; QtWebKit和V8不共享相同的js环境(和事件循环),因此您的node.js应用程序将无法加载和使用CasperJS模块.您必须使用子进程调用单独运行CasperJS脚本, 就像在github上一样.我不打算让CasperJS与phantomjs-node兼容,因为它使用了 alert()基于脏的黑客,我并不容易.

干杯, - 尼古拉斯佩里奥


Hem*_*ela 17

CasperJS包括一个与外界交流的Web服务器.节点(使用request,superagent等等),现在可以跟卡斯帕通过HTTP.

scraper.js:

#!/usr/bin/env casperjs

// I AM NOT NODEJS
// I AM CASPER JS
// I RUN IN QTWEBKIT, NOT V8

var casper = require('casper').create();
var server = require('webserver').create();
var ipAndPort = '127.0.0.1:8585';

server.listen(ipAndPort, function(request, response) {

    casper.start('https://connect.data.com/login');
    casper.userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36");
    casper.then(function(){
        // lots of code here, and a few more cassper.then()s
    });

    casper.run(function(){
        console.log('\n\nFinished')
        response.statusCode = 200;
        var body = JSON.stringify({
            phoneNumber: '1800-YOLO-SWAG'
        })

        response.write(body);
        response.close();
    });
});
Run Code Online (Sandbox Code Playgroud)

您现在可以scraper.js作为Web服务器运行:

chmod +x scraper.js
./scraper.js
Run Code Online (Sandbox Code Playgroud)

您应该像运行节点应用程序一样将其作为Linux服务运行.