简体   繁体   English

Cheerio:如何按文本内容选择元素?

[英]Cheerio: How to select element by text content?

I have some HTML like this:我有一些这样的 HTML:

<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();

But this doesn't work (the innerHTML modifier does not select anything).但这不起作用( innerHTML修饰符不选择任何内容)。

Any clue?有什么线索吗?

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).您的代码不起作用的原因是因为[innerHTML]是属性选择器,而innerHTML不是元素的属性(这意味着未选择任何内容)。

You could filter the span elements based on their text.您可以根据文本过滤span元素。 In the example below, .trim() is used to trim off any whitespace.在下面的示例中, .trim()用于删除任何空白。 If the text equals 'Category:', then the element is included in the filtered set of returned elements.如果文本等于“Category:”,则该元素包含在过滤后的返回元素集中。

var category = $('span').filter(function() {
  return $(this).text().trim() === 'Category:';
}).next().text();

The above snippet will filter elements if their text is exactly 'Category:'.如果元素的文本恰好是“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):如果你想在文本包含该字符串的情况下选择元素,你可以使用:contains选择器(如评论中指出的那样):

var category = $('span:contains("Category:")').next().text();

Alternatively, using the .indexOf() method would work as well:或者,使用.indexOf()方法也可以:

var category = $('span').filter(function() {
  return $(this).text().indexOf('Category:') > -1;
}).next().text();

A simpler solution is:一个更简单的解决方案是:

var category = $('span:contains("Category:") + span').text()

This is css plus the :contains pseudo that is part of jQuery and supported by cheerio.这是 css 加上:contains伪,它是 jQuery 的一部分并由 cheerio 支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM