89961330 2019-06-30
Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Puppeteer runs headless by default, but can be configured to run full (non-headless) Chrome or Chromium.
一开始按照往常的套路(npm install --save puppeteer
) 好几次都卡住了,后期查找资料发现切换到国内的镜像就可以顺利下载
PUPPETEER_DOWNLOAD_HOST=https://storage.googleapis.com.cnpmjs.org npm i --save puppeteer
To use Puppeteer in your project, run:
PUPPETEER_DOWNLOAD_HOST=https://storage.googleapis.com.cnpmjs.org npm i --save puppeteer
Install some basic packages
npm install
const puppeteer = require('puppeteer'), fs = require('fs'), path = require('path'), request = require('request') function mkdirsSync(dirname) { if (fs.existsSync(dirname)) { return true } else { if (mkdirsSync(path.dirname(dirname))) { fs.mkdirSync(dirname) return true } } } var snapShotFolerPath = path.join(__dirname, '../snspshot/') mkdirsSync(snapShotFolerPath) function snapShot (taskInfo) { return new Promise(function (resolve, reject) { (async function(){ // 启动Chromium const browser = await puppeteer.launch({ignoreHTTPSErrors: true, headless:true, args: ['--no-sandbox']}) // 打开新页面 const page = await browser.newPage() // 设置页面分辨率 await page.setViewport({width: 1920, height: 1080}) // 访问 await page.goto(taskInfo.websiteUrl, {waitUntil: 'domcontentloaded'}).catch(err => console.log(err)) await page.waitFor(1000) try { // 截图 await page.screenshot({path: snapShotFolerPath + taskInfo.imageName, fullPage:true}).catch(err => { console.log('截图失败: ' + err) }); await page.waitFor(6000) } catch (e) { console.log('failed ' + e) } finally { await browser.close() fs.stat(snapShotFolerPath + taskInfo.imageName, function(err,stats){ if (err) { reject('fail') } else { if (stats.isFile()) { resolve('success') } } }) } })() }) } module.exports = snapShot
如果你有翻墙环境
如果你没有翻墙环境
while 循环去调用接口去获取当前的截图任务
如果有截图任务那么就去截图
Demo 中执行 npm start
真正执行的是 quickStart.js 中的代码。完整的“获取截图任务、截图、截图上传到OSS、失败则将失败任务上传到服务”逻辑在 index.js 文件中