Cheerio 错误,未定义不是函数 // 这是正确的方法吗?

Fur*_*dew 5 node.js cheerio

尝试运行代码时,我不断收到错误$.find('.market_listing_item_name_block').each()- undefined 不是函数,指向 find。我认为 find 是cheerio中的一个函数?公平地说,我不确定我是否做对了,这是我的代码:

var cheerio = require('cheerio')
$ = cheerio.load('#searchResultsRows')
var url = 'http://steamcommunity.com/market/search?appid=730'
xhr.get(url).success(function(r){
    $.find(".market_listing_item_name_block").each(function(){
        var name = $(this).find(".market_listing_item_name").text();
        console.log(name)
    })
})
Run Code Online (Sandbox Code Playgroud)

xhr 是一个本质上类似于 AJAX 的对象。

我之前在 chrome 中的做法是:

var url = 'http://steamcommunity.com/market/search?appid=730'
var itemDiv = $("<div></div>")
$.get(url).success(function(r){
    d = $(r).find('#searchResultsRows')
    itemDiv.append(d)
})
Run Code Online (Sandbox Code Playgroud)

进而:

itemDiv.find(".market_listing_item_name_block").each(function(){
   var name = $(this).find(".market_listing_item_name").text();
   console.log(name) // would normally do other things, but for the purpose of this post, i'm just console logging the name
})
Run Code Online (Sandbox Code Playgroud)

我究竟如何才能在 node/cheerio 中重新创建那个 ^?我相信我显然错过了几步。非常感谢任何帮助,谢谢。

onm*_*133 7

当你得到一个元素时,你需要$在做进一步的选择之前使用 wrap 该元素

const $ = Cheerio.load(body)
const list = $('div.titles li')

list.each((index, li) => {
  const title = $(li).find('p.title').text()

  console.log(title)
})
Run Code Online (Sandbox Code Playgroud)