首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cheerio WebScraping节点JS

Cheerio WebScraping节点JS
EN

Stack Overflow用户
提问于 2021-07-23 01:59:52
回答 3查看 110关注 0票数 1

我想要抓取一个网页,并得到所有的链接(如果有)一个缩略图或图像内的'a‘标签。

我能够获得链接,但不确定如何在当前的标签im迭代中获取img > src值。

代码语言:javascript
复制
const cheerio = require('cheerio')
const request = require('request')
const throttledRequest = require('throttled-request')(request)
throttledRequest.configure({ requests: 18, milliseconds: 1000 })

let o = {
  linksOut: []
}

const scrapeLinksOut = (o, body) => {
  if (body) {
    let $ = cheerio.load(body)

    $('a').map(function () {
      let link = $(this).attr('href')
      // I want to get the img url within the a tag for the current iteration
      let thumbnail = $(this).//img > src

      o.linksOut.push( {
        link: link,
        thumbnail: thumbnail
      })
    })
  } else {
   // something else
  }
}

const scrape = (() => {
  return new Promise((resolve, reject) => {
    throttledRequest({
      url: 'https://www.ibm.com/us-en',
      followAllRedirects: true,
      timeout: 30000
    }, (err, res, body) => {
      scrapeLinksOut(o, body)
      return resolve(o)
    })
  }) 
})

scrape()
  .then((res) => {
    res.linksOut.forEach((obj) => {
     console.log(obj);
   })
  })
  .catch((err) => console.log(err))
EN

回答 3

Stack Overflow用户

发布于 2021-07-23 10:26:58

您可以使用find()获取img元素,然后使用attr()获取其src。

$(this).find('img').attr('src')

票数 0
EN

Stack Overflow用户

发布于 2021-07-23 11:03:39

我写了一个快速的解决方案,希望能解决你的问题。我在我的机器上测试了它,它对我来说是有效的。

代码语言:javascript
复制
$('a').map(function () {

...
    let thumbnail = $(this).html();
    thumbnail = cheerio.parseHTML(thumbnail);

    if (thumbnail!== null && thumbnail.length > 0 && //Ensure link has an image
    thumbnail[0].attribs !== undefined && 
    thumbnail[0].attribs.src !== undefined) {
        thumbnail = thumbnail[0].attribs.src; //The image URL
    }

...

});
代码语言:javascript
复制
const scrapeLinksOut = (o, body) => {
  if (body) {
    let $ = cheerio.load(body)

    $('a').map(function () {
      let link = $(this).attr('href')
     
      let thumbnail = $(this).html();
      thumbnail = cheerio.parseHTML(thumbnail)


      if (thumbnail !== null && thumbnail.length > 0 && thumbnail[0].attribs !== undefined && thumbnail[0].attribs.src !== undefined) {
        thumbnail = thumbnail[0].attribs.src
      } else {
        thumbnail = null
      }

      o.linksOut.push( {
        link: link,
        thumbnail: thumbnail
      })
    })
  } else {
    o.linksOut.push(links)
  }
}

这记录了:

代码语言:javascript
复制
null
null
null
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
null
null
null
null
null
null
null
票数 0
EN

Stack Overflow用户

发布于 2021-07-23 12:34:28

这似乎是可行的。链接和图像匹配。

代码语言:javascript
复制
const scrapeLinksOut = (o, body) => {
  if (body) {
    let $ = cheerio.load(body);

    $('a').map(function () {
      let thumbnail;
      let link = $(this).attr('href');

      $(this)
        .find('img')
        .map(function (i, img) {
          thumbnail = $(img).attr('src');
        });

      if (thumbnail !== undefined) {
        o.linksOut.push({
          link: link,
          thumbnail: thumbnail,
        });
      }
    });
  } else {
    // something else
  }
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68493198

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档