步骤
- 安装
- 编写test.js脚本
- 执行
安装
官网:https://pptr.nodejs.cn/
# 如果没有Node.js环境要先安装Node
npm i puppeteer
编写test.js脚本
const url='https://***.com/test';
console.log('url',url)
const puppeteer = require('puppeteer');
(async () => {
// 创建浏览器
// const browser = await puppeteer.launch();
// 如果是在服务器上用root来执行,必须加上--no-sandbox参数
const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] })
// 创建请求页面
const page = await browser.newPage();
// 监听页面的日志并打印出来
page.on('console', msg => console.log( msg.text()));
// 设置页面视口大小
// page.setViewport({ width: 800, height: 800, deviceScaleFactor: 1});
// 开始访问页面
await page.goto(url);
// 页面截图
// await page.screenshot({path: 'example3.png'});
// 关闭浏览器
// await browser.close();
})();
执行
# 通过Node.js来运行脚本
nohup node test.js &