【发布时间】:2019-02-11 20:58:25
【问题描述】:
我使用Cheerio 来查找网页中最大的图像。这是我使用的代码:
const { src } = $('img')
.map((i, el) => ({
src: el.attribs.src,
width: el.attribs.width ? Number(el.attribs.width.match(/\d+/)[0]) : -1,
}))
.toArray()
.reduce((prev, current) => (prev.width > current.width ? prev : current));
但是,它只有在 with width 是 inline 的 img 时才有效。如果没有宽度,我会将其宽度设置为-1 并在排序时考虑它
有没有办法在没有这些黑客的情况下使用 Puppeteer 在网页中找到最大的图像?由于浏览器正在渲染所有这些,它可以很容易地找出哪个是最大的
【问题讨论】:
标签: javascript node.js puppeteer cheerio