efficient way to divide ignoring rest
Tag : cpp , By : yew tree
Date : March 29 2020, 07:55 AM
wish helps you there are 2 ways i found to get a whole number from a division in c++ , If you're dealing with integers, then the usual way is Quotient = value1 / value2;
|
How to download PDF file using PUPPETEER after form submit? NodeJS | PUPPETEER
Tag : node.js , By : Sergio Rudenko
Date : March 29 2020, 07:55 AM
wish helps you I do not know exactly how the pdf is downloaded but I think you can use the following. await page._client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: '/home/me/stuff'});
|
What's the performance difference of puppeteer.launch() versus puppeteer.connect()?
Tag : docker , By : Ivan Belov
Date : March 29 2020, 07:55 AM
seems to work fine Short Answer: Using puppeteer.connect() / browser.disconnect() whenever possible is best from a performance standpoint and is ≈ 146 times faster than using puppeteer.launch() / browser.close() (according to my benchmark tests). 'use strict';
const puppeteer = require('puppeteer');
const { performance } = require('perf_hooks');
const iterations = 10000;
(async () => {
let browser;
const start_time = performance.now();
for (let i = 0; i < iterations; i++) {
browser = await puppeteer.launch();
await browser.close();
}
const end_time = performance.now();
const total_time = end_time - start_time;
const average_time = total_time / iterations;
process.stdout.write (
'Total Time:\t' + total_time + ' ms\n'
+ 'Average Time:\t' + average_time + ' ms\n'
+ 'Iterations:\t' + iterations.toLocaleString() + '\n'
);
})();
'use strict';
const puppeteer = require('puppeteer');
const { performance } = require('perf_hooks');
const iterations = 10000;
(async () => {
let browser = await puppeteer.launch();
const browserWSEndpoint = browser.wsEndpoint();
browser.disconnect();
const start_time = performance.now();
for (let i = 0; i < iterations; i++) {
browser = await puppeteer.connect({
browserWSEndpoint,
});
browser.disconnect();
}
const end_time = performance.now();
const total_time = end_time - start_time;
const average_time = total_time / iterations;
process.stdout.write (
'Total Time:\t' + total_time + ' ms\n'
+ 'Average Time:\t' + average_time + ' ms\n'
+ 'Iterations:\t' + iterations.toLocaleString() + '\n'
);
process.exit();
})();
|
Can you input into the text field of a prompt box using Puppeteer?
Date : March 29 2020, 07:55 AM
With these it helps You have to use the dialog event. Once there, you can use dialog.accept to set the text. This will input "Stack Overflow" in your example: const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_prompt");
const frame = (await page.frames())[1];
page.on('dialog', dialog => {
dialog.accept("Stack Overflow!");
});
await frame.click("BUTTON")
browser.close();
|
Calling puppeteer from rest api
Date : March 29 2020, 07:55 AM
seems to work fine Yes, it is possible to do that. I recommend to use a pool of puppeteer instances to limit the number of parallel executions. As you already noticed, otherwise you might have 10 open browsers in the case of 10 requests coming in at roughly the same time. Check out the library puppeteer-cluster (disclaimer: I'm the author), which supports your use case. const express = require('express');
const app = express();
const { Cluster } = require('puppeteer-cluster');
(async () => {
const cluster = await Cluster.launch({
concurrency: Cluster.CONCURRENCY_BROWSER,
maxConcurrency: 4,
});
// setup the function to be executed for each request
await cluster.task(async ({ page, data: url }) => {
await page.goto('http://' + url);
// ...
return await page.content();
});
// setup server
app.get('/', async function (req, res) { // expects URL to be given by ?url=...
try {
// run the task function for the URL
const resp = await cluster.execute(req.query.url);
// respond with the result
res.status(200).send(resp);
} catch (err) {
// catch error
res.end('Error: ' + err.message);
}
});
app.listen(3000, function () {
console.log('Server listening on port 3000.');
});
})();
|