admin 管理员组文章数量: 1086019
let playersCell = `
<td class="foo" colspan="2">
<a href="example">
<span class="bold">John Beluga</span>
- Sarah Jay.
</a>
</td>
`
let players = cheerio.load(playersCell)
players.find('a').html()
I try to load a html string into cheerio.js and find an a
tag, but I am getting
[TypeError: players.find is not a function]
Console.log
shows for players
let playersCell = `
<td class="foo" colspan="2">
<a href="example.">
<span class="bold">John Beluga</span>
- Sarah Jay.
</a>
</td>
`
let players = cheerio.load(playersCell)
players.find('a').html()
I try to load a html string into cheerio.js and find an a
tag, but I am getting
[TypeError: players.find is not a function]
Console.log
shows for players
-
Can you try this
let players = cheerio.load(playersCell); players('.foo').find('a').html()
– Hassan Imam Commented Jun 29, 2017 at 18:15
2 Answers
Reset to default 6I got .find is not a function
, and when I looked at the object in the console that I was trying to find, it said its type was tag
. I realized I needed to wrap the object again.
let results = $('.your .query')
results.each((i, r) => {
$(r).find('.your .next .query')
})
find
is a method that appears on DOM search results. You need to create a result before you can use find.
For example:
let playersCell = `<table><tr>
<td class="foo" colspan="2">
<a href="example.">
<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>
But in this case, there is no need to. You can just use the initial search directly:
let playersCell = `
<td class="foo" colspan="2">
<a href="example.">
<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>
本文标签: javascriptfind is not a function on cheerio objectStack Overflow
版权声明:本文标题:javascript - .find is not a function on cheerio object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744086130a2531161.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论