从网页获取链接

时间:2019-09-25 18:06:31

标签: jquery node.js web-scraping

我有一个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);

  }
})

2 个答案:

答案 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()