有没有办法让jquery与node.io一起工作?

有没有办法让jquery与node.io一起工作?,jquery,node.js,Jquery,Node.js,Node.js noob。我使用node.io来抓取网站,但我希望在node.io中使用jquery。node.io提供的功能没有提供太多的灵活性 var nodeio = require('node.io'), options = {timeout: 10}, jQuery = require('jquery'); exports.job = new nodeio.Job(options, { input: ['hello', 'foobar', 'weather'],

Node.js noob。我使用node.io来抓取网站,但我希望在node.io中使用jquery。node.io提供的功能没有提供太多的灵活性

var nodeio = require('node.io'), options = {timeout: 10},
    jQuery = require('jquery');

exports.job = new nodeio.Job(options, {
    input: ['hello', 'foobar', 'weather'],
    run: function (keyword) {
        this.getHtml('http://www.google.com/search?q=' + encodeURIComponent(keyword), function (err, $) {

            // SOMEHOW CREATE THE JQUERY OBJECT USING $

            var results = $('#resultStats').text.toLowerCase();
            this.emit(keyword + ' has ' + results);
        });
    }
});
有人能帮忙吗

更新

我没有注意到node.io有一个将jquery与
jsdom:true
选项一起使用的选项。即使使用此选项,它也不起作用,我总是会收到超时错误或$is undefined错误

看看-一个模仿JS中DOM的模块,允许您使用为浏览器设计的任何JS库


这篇博文解释了如何将它与jQuery结合起来:。

你可以看看mikeal使用spider——它支持这个开箱即用的用例,并且是为抓取而构建的

例如:

var spider = require('../main');

spider()
.route('www.nytimes.com', '/pages/dining/index.html', function (window, $) {
  $('a').spider();
})
.route('travel.nytimes.com', '*', function (window, $) {
  $('a').spider();
  if (this.fromCache) return;

  var article = { title: $('nyt_headline').text(), articleBody: '', photos: [] }
  article.body = '' 
  $('div.articleBody').each(function () {
    article.body += this.outerHTML;
  })
  $('div#abColumn img').each(function () {
    var p = $(this).attr('src');
    if (p.indexOf('ADS') === -1) {
      article.photos.push(p);
    }
  })
  // console.log(article);
})
.route('dinersjournal.blogs.nytimes.com', '*', function (window, $) {
  var article = {title: $('h1.entry-title').text()}
  // console.log($('div.entry-content').html())
})
.get('http://www.nytimes.com/pages/dining/index.html')
.log('info')
;
另外,如果您打算使用node.io——我认为node.io将数据作为可选参数传递:

io.getHTML('someurl', function(err, junk, data){
    jsdom.env({
      html: data,
      scripts : [
        'http://code.jquery.com/jquery-1.5.min.js'
      ]
    }, function(err, window) {
          var $ = window.jQuery;
          // use jquery here
   });
});

好的,解决问题了

io有一个使用jquery的选项,您需要传递这个选项
jsdom:true
。我使用的node.io(0.3.0)版本中有一个bug,它总是返回超时

  // lib/node.io/dom.js
  window.onload = function() {
    callback.apply(self, [null, $, data, headers, response]);
  }
现在node.io版本0.3.1中修复了这一问题


谢谢大家的回复

我了解jsdom,我特别需要帮助将node.io的$window对象分配给jquery或jsdom对象,如上图所示,这样我就可以使用jquery了。对不起,你的回答没用@Madhusudhan,你可以用你想要的任何东西替换
$
。关键是,如果不完全模拟浏览器DOM,jQuery将无法工作,这就是为什么需要jsdom。你可以阅读我链接到的博客文章来获得帮助。实际上我看不到这一点,node.io有一个使用jquery的选项。默认选项是jsdom:false,当您传递true时,您将获得以$表示的jquery对象。不知道为什么,当我使用jsdom:true时,会抛出一个错误,说$is undefined