问题
This program works well on puppeteer when i use it on CMD. Although, it is a lengthy process and also complicated for any non-technical person. I want to make an exe file that perform the task I do manually to run this node.js file in CMD. As you can see first my program will open the browser and goto a (URL). I want to make different program with different URL. So that, if a person want to run this code, he just click the exe file and then that software will automatically do that task for the user.
const puppeteer = require('puppeteer');
async function getPic() {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.setViewport({width: 2576, height: 4134})
await page.goto('http://absoluteindianews.com/epaper-
en/index.php/epaper/edition/906/delhi-edition');
for (let i=1; i<=8; i++){
await page.click('#page_area > a > img');
await page.waitFor(4000);
await page.screenshot({path: 'C:/Users/biznis/Desktop/automatic
downloading/Puppeteer/AbsoluteIndia/Delhi/Delhi'+ i +'.png'});
await page.waitFor(2000);
await page.click('#cboxLoadedContent > img');
await page.waitFor(2000);
if(i<8) {
await page.click('#yw1 > li.next > a');
}
await page.waitFor(2000);
};
await page.setViewport({width: 2576, height: 4134})
await page.goto('http://absoluteindianews.com/epaper-
en/index.php/epaper/edition/905/mumbai-edition');
for (let i=1; i<=8; i++){
await page.click('#page_area > a > img');
await page.waitFor(4000);
await page.screenshot({path: 'C:/Users/biznis/Desktop/automatic
downloading/Puppeteer/AbsoluteIndia/Mumbai/Mumbai'+ i +'.png'});
await page.waitFor(2000);
await page.click('#cboxLoadedContent > img');
await page.waitFor(2000);
if(i<8) {
await page.click('#yw1 > li.next > a');
}
await page.waitFor(2000);
};
await page.setViewport({width: 2576, height: 4134})
await page.goto('http://absoluteindianews.com/epaper-
en/index.php/epaper/edition/904/bhopal-absolute');
for (let i=1; i<=8; i++){
await page.click('#page_area > a > img');
await page.waitFor(4000);
await page.screenshot({path: 'C:/Users/biznis/Desktop/automatic
downloading/Puppeteer/AbsoluteIndia/Bhopal/Bhopal'+ i +'.png'});
await page.waitFor(2000);
await page.click('#cboxLoadedContent > img');
await page.waitFor(2000);
if(i<8) {
await page.click('#yw1 > li.next > a');
}
await page.waitFor(2000);
};
await browser.close();
}
getPic();
回答1:
There are multiple ways to resolve this and writing them into single answer is not possible. However I can provide some guideline above nexe
and electron
. There are also enclosejs
and pkg
as well.
In both solution below, one of the most important rule is not to bundle your node_modules folder. Chromium binary will not work if you bundle it.
Nexe
You can use nexe. Which will download and bundle your nodejs script into a single executable file. Install it globally,
npm i -g nexe
Then create your puppeteer script. here is a sample file,
const puppeteer = require("puppeteer");
async function scraper(url) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
const title = await page.title();
await browser.close();
return title;
}
scraper("http://example.com").then(console.log);
Now bundle it using,
nexe index.js
Finally copy the node_modules folder and the bundled executable file to your client.
Electron
You can have a nice GUI with electron and create a executable file using electron-builder.
PS: The GUI is optional, and not part of this answer. It's just to show how you can have executable file for your client which can do much more than just run the browser.
I won't go into what is electron and how it works, instead I'll use a quickstart example. If you want to have final code, check this repo.
First clone the quick start repo,
git clone https://github.com/electron/electron-quick-start
Then install puppeteer and electron-builder,
yarn add puppeteer
npm i -g electron-builder
Now edit the main.js
and add nodeIntegration: true
to webPreferences
,
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true // <-- this line
}
})
Now edit index.html
and add a button and result container,
<p><button>Get Result</button><div id="result"></div></p>
Edit renderer.js
and paste the sample code we used on nexe example. Additionally use these lines,
document.querySelector("button").addEventListener("click", async function() {
const result = await scraper("http://example.com");
document.querySelector("#result").innerHTML = result;
});
Now open package.json and add these options so we can run the chromium binary file,
"build": {
"extraResources": "node_modules",
"files": [
"!node_modules"
]
}
Now build the app,
electron-builder
Open dist
folder and you will get your packages apps. You can run and get the result,
来源:https://stackoverflow.com/questions/56810069/is-there-any-way-i-can-execute-my-node-js-and-puppeteer-program-with-an-exe-file