从网页获取链接
我有一个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);
}
})
回答如下:读取href
属性值以获取链接
inksList.push(bodyHtml.find('a').attr('href'));
从网页获取链接
我有一个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);
}
})
回答如下:读取href
属性值以获取链接
inksList.push(bodyHtml.find('a').attr('href'));