【问题标题】:function in coffeescript on cheerio object not running在cheerio对象上没有运行的coffeescript中的函数
【发布时间】:2013-07-03 23:35:01
【问题描述】:

我有这个代码:

 $ = $root.cheerio.load(body)
 results = $('#selector').map (index, element)->
      items = []
      $(this).find('.item_link').each (i, el)->
        console.log("asdf")
        items[i] =  $(this).attr('title')

      result=
        name: $(this).find('.the_name_link').attr('title') 
        #HERE I'd like to do something with items but it's not working
      return result

    return callback(null,results)

所以当这段代码执行时,基本上没有错误,在我的界面中我得到了一个结果

[
    {
        "name": "The Name I was looking for"
        }
]

但似乎每个块中的代码永远不会被执行(没有控制台输出'asdf')。我做错了什么?

【问题讨论】:

  • 在浏览器的同一页面上使用 jquery 进行类似的调用有效。 .item_link 中有多个元素
  • 你能把body的HTML贴出来吗?您是否探索过$(this).find('.item_link') 根本不匹配任何内容并返回空数组的可能性?
  • 事实上是这样。我想知道为什么。因为如果我在浏览器的控制台中这样做,它不会!我有点困惑……
  • 似乎是cheerio 中的错误?转储元素的整个 html 会遗漏很多 html 元素!奇怪...
  • 同样,如果您发布 HTML,我们可以提供具体帮助。我认为,cheerio 不太可能仅通过 parse->rerender 循环删除已解析 HTML 的元素/属性。无论如何,您的代码都是只读的,因此您不会意外更改 DOM。但无论如何,继续调试过程,将奇怪的部分隔离到越来越小的代码/标记部分,直到你弄明白为止。

标签: function coffeescript cheerio


【解决方案1】:

现在这已经严重误导了。问题是发送到应用程序的错误参数会以不完整的 html 进行回复,因此代码(正确...)没有找到我正在寻找的 div :)

因此上面的咖啡脚本是正确的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 2014-09-21
    • 2013-03-02
    • 2020-12-03
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    相关资源
    最近更新 更多