【发布时间】:2019-08-21 09:17:14
【问题描述】:
我发现无法在 cheerio 元素(版本 1.0.0-rc.3)上应用选择器。使用 find() 会引发错误。
const xmlText = `
<table>
<tr><td>Foo</td><td/></tr>
<tr><td>1,2,3</td><td>number</td></tr>
<tr><td>A,B</td><td>char</td></tr>
<!-- more rows -->
</table>
<table>
<tr><td>Bar</td><td/></tr>
<!-- more rows -->
</table>
`
const $ = cheerio.load(xmlText)
$('table').each((i, table) => {
// TODO if first row contains Foo then map the following rows
if (table.find("td:contains('Foo')").length > 0) {
// ...
}
})
类型“CheerioElement”上不存在属性“find”。
如何应用子元素的选择器?
给定的示例应该只处理带有第一行文本Foo 的表格并返回以下对象列表。
// desired result
[{ value: '1,2,3', type: 'numbner' }, { value: 'A,B', type: 'char' }, ...]
【问题讨论】:
标签: typescript cheerio