browserless puppeteer nodejs示例

  1. 安装 puppeteer插件
    npm install puppeteer -S
  2. 引入puppeteer这个插件   const puppeteer = require('puppeteer');
  3. 两种方式抓取网页快照 用browserless.io国外网站提供的服务。这个服务对测试demo免费。无需注册。
// From inside your Node application
//import puppeteer from 'puppeteer';

const puppeteer = require('puppeteer');
const fs = require('fs');

browserlessScreenshot();
//Screenshot('https://www.cnblogs.com/sanshengshui/p/11066103.html');

async function Screenshot(url) {
   const browser = await puppeteer.launch({
       headless: true,
       args: [
       "--no-sandbox",
       "--disable-gpu",
       ]
   });

    const page = await browser.newPage();
    await page.goto(url, {
      timeout: 0,
      waitUntil: 'networkidle0',
    });
    const screenData = await page.screenshot({encoding: 'binary', type: 'jpeg', quality: 30});
    fs.writeFileSync('screenshot.jpg', screenData);

    await page.close();
    await browser.close();
}


async function browserlessScreenshot() {
    // Replace puppeteer.launch with puppeteer.connect
    const browser = await puppeteer.connect({
        browserWSEndpoint: 'wss://chrome.browserless.io'
    });

    // The rest of your script remains the same
    const page = await browser.newPage();
    await page.goto('https://www.google.com/');
    await page.screenshot({ path: 'screenshot.png' });
    page.close();
}

 

上一篇:Robotframework的截图和报告


下一篇:3小时搞定一个简单的MIS系统案例Northwind,有视频、有源代码下载、有真相