使用puppeteer,如何减少加载同一页面的时间?

时间:2019-01-29 06:56:51

标签: caching puppeteer

对不起,我的英语不好。问题:

我正在设置一个新的puppeteer节点服务器(一个docker容器),用于加载相同的页面,URL参数不相同,并且其中包含一个Qr代码图像。目标正在拍摄此二维码图像的屏幕截图。而且很慢。我使用Chrome手动访问此页面的速度比操纵木偶的人快,有什么想法可以减少操纵木偶的加载时间?谢谢。

server.js是快递

const puppeteer = require('puppeteer');
const express = require('express')

const fs = require('fs');
const bodyParser = require('body-parser');

const app = express()
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json({limit: '20mb'}))

var express_timeout = require('connect-timeout'); //express v4

app.use(express_timeout(120000000));
app.use(haltOnTimedout);

function haltOnTimedout(req, res, next){
  if (!req.timedout) next();
}

let browser = null;
let browser2 = null;
let quickpage = null;

(async() => {
  browser = await puppeteer.launch({
    args: ['--no-sandbox', '--disable-setuid-sandbox'],
    executablePath: '/usr/local/lib/chrome-linux/chrome',
    userDataDir: '/tmp/user-data-dir',
    headless: true,
  });
  browser2 = await puppeteer.launch({
    args: ['--no-sandbox', '--disable-setuid-sandbox'],
    executablePath: '/usr/local/lib/chrome-linux/chrome',
    userDataDir: '/tmp/user-data-dir',
    headless: true,
  });

  quickpage = await browser.newPage()
  await quickpage.setViewport({ width: 1024, height: 768 })
  await quickpage.setCacheEnabled(true)
})();


app.post('/htmlOperxxxe/xxx', function (req, res) {
  (async() => {
    try {
      console.log(req.body)
      let random_file_name = function (ext, ppath) {
        if (!ext) {
          ext = 'png'
        }
        if (!ppath) {
          ppath = 'ipxxxne/'
        }
        return ppath + (new Date()).getTime() + random_str(5) + '.' + ext
      }
      let time_out = req.body.time_out
      let bank_val = req.body.bank_val

      if (!bank_val) {
        bank_val = 'xxxNL2A'
      }
      if (!time_out && time_out !== 0) {
        time_out = 30000
      }

      let bank_page_info = {
        'xxxANL2A' : {'selector': '.qrcode'},
        'IxxxxL2A' : {'selector': '#qrcode'},
        'FVLxxxx22' : {'selector': ''},
      }

      let page = await browser.newPage()
      await page.setViewport({ width: 1280, height: 800 })
      await page.setCacheEnabled(true)

      console.log('init')
      await page.goto(req.body.addr, {waitUntil: 'networkidle2', timeout: time_out})
      await page.screenshot({ path: random_file_name(), fullPage: false })
      console.log('1 done')

      let ideal_img_btn = '#paymentMethodBlock > div > img:nth-child(8)'
      await page.waitForSelector(ideal_img_btn, { timeout: time_out})
      await page.tap(ideal_img_btn);


      let bank_select = '#idealIssuer'
      await page.waitForSelector(bank_select, { timeout: time_out})
      await page.select(bank_select, bank_val)
      await page.screenshot({ path: random_file_name(), fullPage: false })
      console.log('2 done')

      let next_btn = '#nextBtn'
      await page.waitForSelector(next_btn, { timeout: time_out})
      await page.tap(next_btn)

      let qr_code_selector = bank_page_info[bank_val].selector
      await page.waitForSelector(qr_code_selector, { timeout: time_out})
      await sleep(350)
      console.log('3 done')

      filename = random_file_name()
      // await page.screenshot({ path: filename, fullPage: false })
      let qrcode_element = await page.$(qr_code_selector);
      await qrcode_element.screenshot({
          path: filename
      });
      await page.close();

      let stats = fs.statSync(filename);
      res.set({
        'Content-Type': 'application/octet-stream',
        'Content-Disposition': 'attachment; filename='+filename,
        'Content-Length': stats.size
      });
      // fs.createReadStream(filename).pipe(res);

      // fs.unlinkSync(filename);

      var file = fs.createReadStream(filename);
      file.on('end', function() {
        fs.unlink(filename, function() {
          // file deleted
        });
      });
      file.pipe(res);
    } catch (e) {
      console.log('Error occurred', e);
      res.status(500).send('PDF generated error:' + e)
    }
  })();
})

app.listen(9020, function () {
  console.log('server is listening on port 9020!')
})

0 个答案:

没有答案