我有一个URL,我想收集链接并将其推送到数组中。但是它将所有内容一起写入一个数组中。 “ .cb-content”是具有链接的div的类。
const express = require('express');
const bodyParser = require('body-parser');
const cheerio = require('cheerio')
const request = require('request')
request(url, (error, response, body) => {
if (!error) {
let linksList = []
$ = cheerio.load(body);
const bodyHtml = $('.cb-content')
$('.cb-content').each(function () {
linksList.push(bodyHtml.find('a').text())
});
console.log(linksList);
}
})
答案 0 :(得分:0)
读取href
属性值以获取链接
inksList.push(bodyHtml.find('a').attr('href'));
答案 1 :(得分:0)
它应该看起来像这样:
let linksList = $('.cb-content a').map((i, a) => {
return $(a).attr('href')
}).get()