- 安装 puppeteer插件
npm install puppeteer -S - 引入puppeteer这个插件
const puppeteer = require('puppeteer');
- 两种方式抓取网页快照 用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(); }