cheerio jquery 节点 js:获取 href 值

cheerio jquery node js: obtaining href value

Source

想要 href 值,所以在这种情况下:link。

所有遵循 .product-name class.

中相同模式的值都需要它

尝试了 return 空白 space/nothing 的方法。

回答后,请简要概述一些 cheerio/jquery 规则,因为我不理解它们。

仅供参考:const $ = cheerio.load(response) //page source.

试试这个:

$('.product-name a[href]').each((index, elem)=>{
    console.log(index, $(elem).attr('href'));
});
/** .product-name : choose class .product-name
 *  .product-name a : choose tag a is child of class product-name
 *  and a[href] : choose tag a has attribute href, if you don't need, you can remove it.
 **/

第一个 link:

let firstElem = $('.product-name a[href]').get()[0]
console.log($(firstElem).attr('href'));