.find 不是 cheerio 对象上的函数

.find is not a function on cheerio object

  let playersCell = `
    <td class="foo" colspan="2">
      <a href="example.com">
        <span class="bold">John Beluga</span>
         - Sarah Jay.
       </a>
    </td>
    `

let players = cheerio.load(playersCell)
players.find('a').html()

我尝试将 html 字符串加载到 cheerio.js 并找到 a 标签,但我得到

[TypeError: players.find is not a function]

Console.log 显示 players

find 是一种出现在 DOM 搜索结果中的方法。您需要先创建一个结果,然后才能使用查找。

例如:

let playersCell = `<table><tr>
    <td class="foo" colspan="2">
      <a href="example.com">
        <span class="bold">John Beluga</span>
         - Sarah Jay.
       </a>
    </td></tr></table>
    `

let players = cheerio.load(playersCell);
console.log(players('td').find('a').html());
<script src="https://wzrd.in/standalone/cheerio@latest"></script>

但在这种情况下,没有必要。您可以直接使用初始搜索:

let playersCell = `
    <td class="foo" colspan="2">
      <a href="example.com">
        <span class="bold">John Beluga</span>
         - Sarah Jay.
       </a>
    </td>
    `

let players = cheerio.load(playersCell);
console.log(players('a').html());
<script src="https://wzrd.in/standalone/cheerio@latest"></script>

我得到 .find is not a function,当我在控制台中查看我试图查找的对象时,它说它的类型是 tag。我意识到我需要再次包装对象。

let results = $('.your .query')
results.each((i, r) => {
  $(r).find('.your .next .query')
})