Puppeteer installation and Failure Reasons

Under the new working path myPuppeteer computer folder, enter cmd into the command line window in the path, enter

npm init

 

Some initial information will pop up to fill the need, do not want to fill may have been built by enter the default initialization file generated at this time package.json success

The next direct input npm i puppeteer directly install the library, the following error will be reported, because the installation puppetteer of default will download Chromium, this has 144M, the file is too large, easily lead to failure download

1, replacing domestic Chromiuminput source after npm i puppeteer

PUPPETEER_DOWNLOAD_HOST=https://storage.googleapis.com.cnpmjs.org

 

Method 2, skip download Chromium can enter --ignore-scripts, or SET = PUPPETEER_SKIP_CHROMIUM_DOWNLOAD . 1

npm i  puppeteer --ignore-scripts

And then manually download Chromium, need to quickly download software over the wall, download the following address displayed to download the corresponding version based on your current system

https://download-chromium.appspot.com/

After the download is complete extract to the root directory of the project

 Demo.js new root directory, input the sample code, attention to the need to store the address configuration Chormium

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({
    executablePath:   'G:\\web\\myPuppeteer\\chrome-win\\chrome.exe',
    args: ['--no-sandbox'],
    dumpio: false 
  });
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await page.screenshot({path: 'example.png'});
  await browser.close();
})();

 

Finally, the sample code execution path in the run command node demo.js

node demo.js

 

Run successfully, the root directory files generated by example.png effect is as follows

 

Guess you like

Origin www.cnblogs.com/zhangyaolan/p/12590028.html