使用puppeteer及puppeteer-custer 做一个简单的网页渲染服务,puppeteer-custer github github.com/thomasdondo… 安装
npm install express
npm install puppeteer
npm install puppeteer-cluster
代码
const express = require('express');
const app = express();
const { Cluster } = require('puppeteer-cluster');
const launchOptions = {
headless: false,
ignoreHTTPSErrors: true, // 忽略证书错误
waitUntil: 'networkidle2',
defaultViewport: {
width: 1920,
height: 1080
},
args: [
'--disable-gpu',
'--disable-dev-shm-usage',
'--disable-web-security',
'--disable-xss-auditor', // 关闭 XSS Auditor
'--no-zygote',
'--no-sandbox',
'--disable-setuid-sandbox',
'--allow-running-insecure-content', // 允许不安全内容
'--disable-webgl',
'--disable-popup-blocking',
//'--proxy-server=http://127.0.0.1:8080' // 配置代理
],
// executablePath: 'xxxx\\chrome.exe',
};
(async () => {
const cluster = await Cluster.launch({
// concurrency: Cluster.CONCURRENCY_CONTEXT,
concurrency: Cluster.CONCURRENCY_PAGE, // 单Chrome多tab模式
maxConcurrency: 1, // 并发的workers数
retryLimit: 2, // 重试次数
monitor: false, // 显示性能消耗
puppeteerOptions: launchOptions
});
await cluster.task(async ({ page, data: url }) => {
await page.goto(url);
const content = await page.content()
// const screen = await page.screenshot();
return content;
});
// setup server
app.get('/', async function (req, res) {
if (!req.query.url) {
return res.end('Please specify url like this: ?url=example.com');
}
try {
const url = req.query.url;
console.log('参数:' + url)
const html = await cluster.execute(url);
// respond with content
res.writeHead(200, {
'Content-Type': 'text/html; charset=UTF-8',
'Content-Length': html.length
});
res.end(html);
} catch (err) {
// catch error
res.end('Error: ' + err.message);
}
});
app.listen(3000, function () {
console.log('Screenshot server listening on port 3000.');
});
})();