Cheerio:如何通过文本内容选择元素? [英] Cheerio: How to select element by text content?
问题描述
我有一些类似这样的HTML:
I have some HTML like this:
<span id="cod">Code:</span> <span>12345</span>
<span>Category:</span> <span>faucets</span>
我想获取类别名称(水龙头"). 这是我的审判:
I want to fetch the category name ("faucets"). This is my trial:
var $ = cheerio.load(html.contents);
var category = $('span[innerHTML="Category:"]').next().text();
但这不起作用(innerHTML
修饰符不会选择任何内容).
But this doesn't work (the innerHTML
modifier does not select anything).
有任何线索吗?
推荐答案
代码不起作用的原因是[innerHTML]
是属性选择器,而innerHTML
不是元素上的属性(这意味着什么都没选择.
The reason your code isn't working is because [innerHTML]
is an attribute selector, and innerHTML
isn't an attribute on the element (which means that nothing is selected).
您可以根据元素的文本过滤span
元素.在下面的示例中,.trim()
用于修剪所有空白.如果文本等于"Category:"(类别:),则该元素将包含在过滤后的返回元素集中.
You could filter the span
elements based on their text. In the example below, .trim()
is used to trim off any whitespace. If the text equals 'Category:', then the element is included in the filtered set of returned elements.
var category = $('span').filter(function() {
return $(this).text().trim() === 'Category:';
}).next().text();
如果元素的文本恰好是类别:",则上述代码段将对其进行过滤.如果要选择元素包含该字符串的元素,则可以使用:contains
选择器(如注释中所指出):
The above snippet will filter elements if their text is exactly 'Category:'. If you want to select elements if their text contains that string, you could use the :contains
selector (as pointed out in the comments):
var category = $('span:contains("Category:")').next().text();
或者,使用.indexOf()
方法也可以:
Alternatively, using the .indexOf()
method would work as well:
var category = $('span').filter(function() {
return $(this).text().indexOf('Category:') > -1;
}).next().text();
这篇关于Cheerio:如何通过文本内容选择元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!