Using puppeter on Linux (CentOS) to realize the function of webpage screenshot

Time:2020-11-28

You may encounter the following problems when installing puppeter on Linux. This article will show you how to climb the pit!

> puppeteer@2.0.0 install /www/node_modules/puppeteer
> node install.js

ERROR: Failed to download Chromium r706915! Set “PUPPETEER_SKIP_CHROMIUM_DOWNLOAD” env variable to skip download.
{ Error: EACCES: permission denied, mkdir ‘/www/node_modules/puppeteer/.local-chromium’
  — ASYNC —
    at BrowserFetcher.<anonymous> (/www/node_modules/puppeteer/lib/helper.js:111:15)
    at Object.<anonymous> (/www/node_modules/puppeteer/install.js:62:16)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
  errno: -13,
  code: ‘EACCES’,
  syscall: ‘mkdir’,
  path: ‘/www/node_modules/puppeteer/.local-chromium’ }
npm WARN enoent ENOENT: no such file or directory, open ‘/www/package.json’
npm WARN www No description
npm WARN www No repository field.
npm WARN www No README data
npm WARN www No license field

The process of installing puppeter and taking actual screenshots are as follows:

1. Install nodejs and NPM

#Download and unzip
wget -c https://nodejs.org/dist/v8.9.1/node-v8.9.1-linux-x64.tar.xz
tar -xvf node-v8.9.1-linux-x64.tar.xz
#Move rename (optional)
mv node-v8.9.1-linux-x64 /www/nodejs
#Create a soft link (shortcut). If the previous Rename is different, the underlined part of this step will be adjusted according to the actual situation
ln -s /www/nodejs/bin/node /usr/local/bin/node
ln -s /www/nodejs/bin/npm /usr/local/bin/npm

2. Install chromium

yum install chromium

3. Install puppeter core (if you install puppeter directly, you will report an error because chrome cannot download it)

npm i puppeteer-core

4. Create a new A.js


const puppeteer = require('puppeteer-core');
(async () => {
 const browser = await puppeteer.launch({
 executablePath: '/usr/bin/chromium-browser',
 args:["--no-sandbox"]
 });
 const page = await browser.newPage();
 await page.goto('https://www.baidu.com');
 await page.screenshot({path: 'example.png'});
 await browser.close();
})();

The code of this step may be different from that on the Internet. One is that the “puppet core” in “require” is not the “puppeter”, because we have installed the “puppet core” instead of the “puppeter”

The other is that there are two parameters in the launch. The first parameter is because we have not directly installed the puppeter, so we need to specify the path. The second parameter is due to some limitations of chromium, which needs to be added to the root account execution

5. Execute A.js

node a.js

After execution, you can see one example.png

Note: if there is Chinese on the page, it may not be displayed normally because of the lack of Chinese font. The solution is to upload the Chinese fonts in C / Windows / fonts of your computer to / usr / share / fonts / Chinese on the server (there is no Chinese directory by default, you can create a new one). If not, please refer to this link https://www.baidu.com/s?ie=utf-8&f=8&rsv_ bp=1&rsv_ idx=1&tn=baidu&wd=centos%20%E4%B8%AD%E6%96%87%E5%AD%97%E4%BD%93&oq=puppeteer&rsv_ pq=a90b2ea50000943a&rsv_ t=9035FHeJQWcr7x6RGsK%2BmDYU11AFyz8svepkcwhlQ51aDVXUfHrKZA7vFjQ&rqlang=cn&rsv_ enter=0&rsv_ dl=tb&inputT=5789&rsv_ sug3=132&rsv_ sug1=63&rsv_ sug7=100&rsv_ sug2=0&rsv_ sug4=5908

Documents related to puppeter https://pptr.dev/

summary

The above is a small part of the introduction to you in Linux (CentOS) using puppeter to achieve the function of webpage screenshot, I hope to help you!

Recommended Today

Regular expression sharing for checking primes

This regular expression is shown as follows: Regular expressions for checking prime numbers or not To use this positive regular expression, you need to convert the natural number into multiple 1 strings. For example, 2 should be written as “11”, 3 should be written as “111”, 17 should be written as “11111111111”. This kind of […]