最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

从网页获取链接

IT培训 admin 3浏览 0评论

从网页获取链接

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

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论