node.js - 如何根据cheerio中的文本获取元素的属性

标签 node.js web-scraping cheerio

(使用 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 .

关于node.js - 如何根据cheerio中的文本获取元素的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63540470/

相关文章:

node.js - node-sass-jspm-importer 路径配置的使用

javascript - Node.JS & Sequelize 定义关系

python - 使用 Scrapy 检测表单(并填写)

html - VBA - 从网页创建表 - 几乎就在那里

javascript - 网页抓取时内存泄漏

javascript - 使用cheerio从ul中提取2个元素

javascript - 无法使 mongoose 过期/ttl 工作

node.js - 为接口(interface)编写 Apollo 解析器

python - 类型错误 : 'FirefoxWebElement' object is not iterable

javascript - 使用 cheerio 查找和替换某些属性