无法在PhantomJS脚本中加载“cheerio”
我在Mac上使用节点版本4.8.1(自制安装),PhantomJS版本2.1.1和cheerio@0.22.0
现在,如果我需要像这样的phantomjs脚本cheerio
// myscript.js var cheerio = require('cheerio'); console.log("done");
并运行该脚本(没有其他的东西)
$ phantomjs myscript.js
然后我会得到这个错误:
TypeError: Object is not a constructor (evaluating 'require("inherits")(Parser, require("events").EventEmitter)') phantomjs://platform/Parser.js:124
我可以用PhantomJS做各种各样的事情。 我唯一不能使用的就是cheerio。
有没有一种方法让我在PhantomJS里面做快乐的工作? 还是可以替代cheerio的工作?
cheerio是一个node.js模块,但PhantomJS不是node.js。 由于cheerio是“服务器的jQuery”,你可能实际上使用真正的jQuery与幻影,包括它到你的目标页面:
http://phantomjs.org/api/webpage/method/include-js.html
page.includeJs('https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js', function() { (page.evaluate(function() { // jQuery is loaded, now manipulate the DOM var $loginForm = $('form#login'); $loginForm.find('input[name="username"]').value('phantomjs'); $loginForm.find('input[name="password"]').value('c45p3r'); })) } );