gpt4 book ai didi

javascript - Node.js Cheerio 模块 ge

转载 作者:行者123 更新时间:2023-11-28 19:11:49 25 4
gpt4 key购买 nike

我正在使用node.js和cheerio模块。我想从页面获取一些数据。

假设我有这种类型的 HTML。

<div class="maindiv">
<h3 class="result-title">
</span>

<a class="link-name" href="/somelink1.html" data-hovercard-id="ds54sdsd4s5d">Name1</a>


</h3>

<h3 class="result-title">
</span>

<a class="link-name" href="/somelink2.html" data-hovercard-id="ds54sdsd4s5d">Name2</a>



</h3>

<h3 class="result-title">
</span>

<a class="link-name" href="/somelink3.html" data-hovercard-id="ds54sdsd4s5d">Name3</a>


</h3>

</div>

如何修改下面的示例来获取链接标记的 html、链接和文本。

request(sub_link, function (error, response, html) {

if (!error && response.statusCode == 200) {

var $ = cheerio.load(html);

$('a.link-name').each(function () {

//var data = (this).html();

var url = this.attr('href');

console.log(url);

links_array.push(url);

});

}
})

最佳答案

这是what the documentation says about each :

Iterates over a cheerio object, executing a function for each matched element. When the callback is fired, the function is fired in the context of the DOM element, so this refers to the current element, which is equivalent to the function parameter element. To break out of the each loop early, return with false.

var fruits = [];

$('li').each(function(i, elem) {
fruits[i] = $(this).text();
});

fruits.join(', ');
//=> Apple, Orange, Pear

所以你必须更换

var url = this.attr('href');

var url = $(this).attr('href');

您在每次迭代时调用 eachpush。您可能应该问自己 map 是否会更好。

关于javascript - Node.js Cheerio 模块 ge,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30620838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com