get() 函数在 cheerio 中有什么作用?

What does the get() function do in cheerio?

有什么区别

$("h1")

$("h1").get()

第一个 returns 某种对象,第二个是数组(具有相同的元素),但它到底是做什么的?

documentation只提到了"Retrieve the DOM elements matched by the Cheerio object. If an index is specified, retrieve one of the elements matched by the Cheerio object",但我还是不太清楚...

$("h1") 生成一个 Cheerio 对象,可以在该对象上使用 Cheerio 方法(例如 .get().text().prop() 等)。在 Cheerio 对象上使用 .get() returns 底层元素的数组 (不是 Cheerio 对象),只能使用该元素支持的方法 - 对于例如,您可以使用 .textContent 而不是 .text(),而不是 .prop(),您可以使用简单的点符号(例如,而不是 $('h1').prop('foo', 'bar')$('h1').get()[0].foo = 'bar')。与 jQuery 的 .get().

相同

Cheerio 对象不是 DOM 元素 - .get() 从前者中提取后者的数组。