使用Puppeter检索网页上所有html img标记的src属性

v8wbuo2f  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(384)

我正在尝试编写一个脚本来定位页面上最大的图像。此过程的第一步是检索特定网站上的所有图像源。这就是我被困的地方。

const puppeteer = require('puppeteer');

function ImageFetcher(pageURL, partName) {
    return new Promise( async (resolve, reject) => {
    try {        
        const browser = await puppeteer.launch({
          headless: false,
        });
        const page1 = await browser.newPage();  
        await page1.goto(pageURL);    

        try {
            const images = await page.$$eval("img", els => els.map(x => x.getAttribute("src")));
            console.log(images);
        } catch(e) {console.log("ERR Locator")};

        await page1.close();
        await browser.close();
        return resolve();
      } catch(e) {console.log(`Error Image Fetcher Part Name: ${partName}`)};
    });
}

async function start() {
    pageURL = "https://www.grainger.com/product/NVENT-CADDY-Cushioned-Pipe-Clamp-1RVC3";
    partName = "10000";
    ImageFetcher(pageURL, partName);
} start();

//ERR Locator
ygya80vv

ygya80vv1#

这个怎么样:

const puppeteer = require("puppeteer");

let testing = async () => {
  const browser = await puppeteer.launch({ 
    headless: false
  });

  const page = await browser.newPage();

  await page.goto('https://www.grainger.com/product/NVENT-CADDY-Cushioned-Pipe-Clamp-1RVC3');

  const image = await extractLargestImage(page);

  return image;
};

async function extractLargestImage(page) {
  return page.evaluate(() => {
    let imgs = document.querySelectorAll('img');
    let largestImgSrc = 'none yet';
    let largestImgSize = 0;

    for (var img of imgs) {
      let imgSize = Number(img.height) * Number(img.width);

      if (imgSize > largestImgSize) {
        largestImgSize = imgSize;
        largestImgSrc = img.src;
      }
    }

    return largestImgSrc;
  });
}

testing().then((value) => {
  console.dir(value, {'maxArrayLength': null});
});

相关问题