【发布时间】:2019-07-15 22:02:55
【问题描述】:
如果我有以下 html 代码:
<a class="my-lovely-name" title="hello" href="fb.com">Random stuff</a>
<div class="my-lovel-name" title="ew" href="yb.com">blabla</div>
如何使用cheerio 提取title 和href 的所有值?我尝试了以下方法,但它只会提取 hello 和 fb.com 并跳过其余元素:
console.log($('.my-lovely-name').attr('title')) => should print the titles of all elements with this class
console.log($('.my-lovely-name').attr('href')) => => should print the href-s of all elements with this class
【问题讨论】:
-
attr()仅返回结果堆栈中第一个元素的值。 github.com/cheeriojs/cheerio#attributes
标签: javascript html node.js cheerio