@ygyg/yg-cli
Version:
A simple CLI for front-end engineering automation construction tool.
39 lines (35 loc) • 1.52 kB
JavaScript
const puppeteer = require('puppeteer');
exports.shot = async function() {
const cfg = { url: 'https://www.ygyg.cn', key: 'ygyg.cn' };
const browser = await puppeteer.launch({
executablePath: '', // 指定浏览器位置,由于下载未成功报相关错误时可自行下载指定位置
headless: true, // 是否弹出浏览器 默认true不弹
});
const page = await browser.newPage();
await page.goto(cfg.url, {
waitUntil: 'networkidle0',
timeout: 0, // 批量生成时以防超时自动中断
});
await page.setRequestInterception(true);
await page.on('requestfinished', (request) => {
// 监听请求结束,更多配置可以在github上查一下
});
await page.waitFor(3000); // 延迟时间毫秒
// 获取页面Dom对象
// let body = await page.$('#xxx'); // 根据id获取需要截取的html也可以根据其他截取或者整个截取
// // 调用页面内Dom对象的 screenshot 方法进行截图
// await body.screenshot({
// path: '/Users/fugang/Documents/puppeteer/img/' + xxx + 'xxx' + '.png',
// }); // 自定义截图位置和名字
// await page.goto('https://example.com');
// await page.screenshot({path: 'example.png'});
// let bodyBig = await page.$('body'); // 截取另一张图
// 调用页面内Dom对象的 screenshot 方法进行截图
await page.screenshot({
path: '' + cfg.key + '.png',
fullPage: true,
});
// await page.pdf({path: 'hn.pdf', format: 'A4'});
await browser.close();
process.exit();
};