如何根据cheerio中的文本获取元素的属性

How to get an attribute of an element based on the text in cheerio

(USING CHEERIO)我想获取在选项标签之间包含文本“9”的元素的值:

<option value="19437843939456"> 8.5 </option>

<option value="19437843972224"> 9 </option>

我想得到这个:19437843972224这是选项标签value属性的值

我如何通过 Cheerio 获得它?

Select the option by using $("option")

Adding the :contains('string') to the selector will look for the text of the option.

Finally the val() method will get the value of the option.

将它们组合起来得到想要的结果:

$("option:contains('9')").val();

这是一个working example

查看 documentation 了解更多信息。

更新

您需要使用正确的 select 元素,您可以使用它的 name 属性:

$("select[name=id] option:contains('9')").val();

更复杂example.