有什么办法可以用exe文件执行Node js和puppeteer程序? [英] Is there any way I can execute my node js and puppeteer program with an exe file?

查看:101
本文介绍了有什么办法可以用exe文件执行Node js和puppeteer程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  const puppeteer = require('puppeteer');异步函数getPic(){const browser =等待puppeteer.launch({headless:false});const page =等待browser.newPage();等待page.setViewport({width:2576,height:4134})等待page.goto('http://absoluteindianews.com/epaper-zh/index.php/epaper/edition/906/delhi-edition');for(让i = 1; i< = 8; i ++){等待page.click('#page_area> a> img');等待页面.waitFor(4000);等待page.screenshot({path:'C:/Users/biznis/Desktop/automatic下载/Puppeteer/AbsoluteIndia/Delhi/Delhi'+ i +'.png'});await page.waitFor(2000);等待页面.click('#cboxLoadedContent> img');await page.waitFor(2000);if(i< 8){await page.click('#yw1> li.next> a');}await page.waitFor(2000);};等待page.setViewport({width:2576,height:4134})等待page.goto('http://absoluteindianews.com/epaper-zh/index.php/epaper/edition/905/mumbai-edition');for(让i = 1; i< = 8; i ++){等待page.click('#page_area> a> img');等待页面.waitFor(4000);等待page.screenshot({path:'C:/Users/biznis/Desktop/automatic下载/Puppeteer/AbsoluteIndia/孟买/孟买'+ i +'.png'});await page.waitFor(2000);等待页面.click('#cboxLoadedContent> img');await page.waitFor(2000);if(i< 8){await page.click('#yw1> li.next> a');}await page.waitFor(2000);};等待page.setViewport({width:2576,height:4134})等待page.goto('http://absoluteindianews.com/epaper-zh/index.php/epaper/edition/904/bhopal-absolute');for(让i = 1; i< = 8; i ++){等待page.click('#page_area> a> img');等待页面.waitFor(4000);等待page.screenshot({path:'C:/Users/biznis/Desktop/automatic下载/Puppeteer/AbsoluteIndia/Bhopal/Bhopal'+ i +'.png'});await page.waitFor(2000);等待页面.click('#cboxLoadedContent> img');await page.waitFor(2000);if(i< 8){await page.click('#yw1> li.next> a');}await page.waitFor(2000);};等待browser.close();}getPic(); 

解决方案

有多种解决方法,无法将其写入单个答案.但是,我可以在 nexe electron 上方提供一些指导.还有 enclosejs pkg .

在以下两种解决方案中,最重要的规则之一是不要捆绑node_modules文件夹.如果将其捆绑,铬二进制文件将不起作用.

Nexe

您可以使用

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();   

解决方案

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,

这篇关于有什么办法可以用exe文件执行Node js和puppeteer程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆