2017-06-29 52 views
1
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標籤,但我正在逐漸.find不cheerio對象的功能

[TypeError: players.find is not a function]

Console.log所示爲players

enter image description here

+0

你可以試試這個'let players = cheerio.load(playersCell); ('。foo')。find('a')。html()' –

回答

1

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/[email protected]"></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/[email protected]"></script>

0

.find is not a function,當我看着在我試圖找到控制檯的對象,它說,它的類型是tag。我意識到我需要再次包裝對象。

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