【问题标题】:JQuery.contents not working using jsdomJQuery.contents 无法使用 jsdom
【发布时间】:2014-10-11 15:46:00
【问题描述】:

这段代码在浏览器中运行,但在 Node.js(使用 JSDom + Crawler)中,我返回 undefined

$('div').find('.class').contents()[0].wholeText

你知道如何解决这个问题吗?

顺便说一句,当我尝试在 nodejs 中对其进行字符串化时,它返回错误Circular defintion

完整代码:

var Crawler = require("crawler").Crawler;
var data = {}
var c = new Crawler({
  maxConnections: 10,
  skipDuplicates: true,
  // This will be called for each crawled page
  callback: function(error, result, $) {

    // $ is a jQuery instance scoped to the server-side DOM of the page
    $("#link > ul > li > a").each(function(index, a) {
      c.queue(a.href);
    });
    $("[id^=enfr]").each(function(index, a){
      var fr = $(this).find('.FrWrd strong').html().replace(/<(?:.|\n)*?>/gm, '')
      var to = $(this).find('.ToWrd').contents()[0]
      console.log(fr + '\t\t\t\t\t' + to)
    })
  }
});

// Queue just one URL, with default callback
c.queue("http://www.wordreference.com/enfr/0");

【问题讨论】:

  • .queue jquery .queue() 吗?谢谢
  • @guest271314 不。它是爬虫的一部分
  • 没试过 node.js , crawler , jsdom 。在没有[0] 的情况下尝试了.contents()? , 或$(this).find('.ToWrd')[0].outerHTML ?

标签: javascript jquery node.js web-crawler jsdom


【解决方案1】:

OK 最后我通过使用“stock”DOM 库解决了这个问题:

  var to = $(this).find('.ToWrd').get(0).firstChild.nodeValue

【讨论】:

    猜你喜欢
    • 2014-05-31
    • 2018-07-27
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 2017-10-13
    • 2020-06-17
    • 1970-01-01
    • 2013-07-28
    相关资源
    最近更新 更多