有什么方法可以使用 exe 文件执行我的 Node js 和 puppeteer 程序吗?

Ham*_*han 2 netbeans cmd exe node.js puppeteer

当我在 CMD 上使用该程序时,该程序在 puppeteer 上运行良好。尽管如此,这是一个漫长的过程,对于任何非技术人员来说也很复杂。我想制作一个 exe 文件来执行我手动执行的任务,以在 CMD 中运行此 node.js 文件。正如您首先看到的,我的程序将打开浏览器并转到(URL)。我想用不同的 URL 制作不同的程序。这样,如果一个人想要运行此代码,他只需单击 exe 文件,然后该软件就会自动为用户执行该任务。

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();   
Run Code Online (Sandbox Code Playgroud)

Md.*_*her 5

有多种方法可以解决此问题,并且不可能将它们写入单个答案。nexe不过我可以在上面提供一些指导electron。还有也有enclosejspkg

在下面的两个解决方案中,最重要的规则之一是不要捆绑您的 node_modules 文件夹。如果捆绑 Chromium 二进制文件,它将无法工作。

内克斯

您可以使用nexe。这将下载您的 Nodejs 脚本并将其捆绑到单个可执行文件中。全局安装,

npm i -g nexe
Run Code Online (Sandbox Code Playgroud)

然后创建你的木偶脚本。这是一个示例文件,

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);
Run Code Online (Sandbox Code Playgroud)

现在将其捆绑使用,

nexe index.js
Run Code Online (Sandbox Code Playgroud)

最后将node_modules文件夹捆绑的可执行文件复制到您的客户端。

电子

您可以使用电子创建一个漂亮的 GUI,并使用电子生成器创建可执行文件。

PS:GUI 是可选的,不是这个答案的一部分。它只是为了展示如何为您的客户端提供可执行文件,它的作用不仅仅是运行浏览器。

我不会讨论什么是电子及其工作原理,而是使用一个快速入门示例。如果您想获得最终代码,请检查此 repo

首先克隆快速启动存储库,

git clone https://github.com/electron/electron-quick-start
Run Code Online (Sandbox Code Playgroud)

然后安装 puppeteer 和 electro-builder,

yarn add puppeteer
npm i -g electron-builder
Run Code Online (Sandbox Code Playgroud)

现在编辑main.js并添加nodeIntegration: truewebPreferences,

mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      nodeIntegration: true // <-- this line
    }
})
Run Code Online (Sandbox Code Playgroud)

现在编辑index.html并添加按钮和结果容器,

<p><button>Get Result</button><div id="result"></div></p>
Run Code Online (Sandbox Code Playgroud)

编辑renderer.js并粘贴我们在下一个示例中使用的示例代码。另外使用这些线,

document.querySelector("button").addEventListener("click", async function() {
  const result = await scraper("http://example.com");
  document.querySelector("#result").innerHTML = result;
});
Run Code Online (Sandbox Code Playgroud)

现在打开 package.json 并添加这些选项,以便我们可以运行 chromium 二进制文件,

"build": {
    "extraResources": "node_modules",
    "files": [
      "!node_modules"
    ]
}
Run Code Online (Sandbox Code Playgroud)

现在构建应用程序,

electron-builder
Run Code Online (Sandbox Code Playgroud)

打开dist文件夹,您将获得您的软件包应用程序。你可以运行并得到结果,

在此输入图像描述