为什么我必须使用 link.attribs.href 而不是标准的 .attr('href') 方法?
...
res.on('end', () => {
const $ = cheerio.load(content);
const link = $('.more-link').get(0);
// const url = link.attr('href'); <--- link.attr is not a function
const url = link.attribs.href; <--- works
console.log(url);
});
根据cheerio 文档,get(i)
从您正在使用的cheerio 实例中检索“DOM”元素。Cheerio 实例对象有一个.attr()
方法,但 DOM 元素只是它们存储该对象数据。
您可以使用.first()
代替.get(0)
.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句