Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Using puppeteer to realize the screenshot function of web page on linux (centos)

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

You may encounter the following problems when installing puppeteer on linux. This article will show you how to climb the hole!

> 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. (/ www/node_modules/puppeteer/lib/helper.js:111:15)

At Object. (/ 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 puppeteer and actually taking screenshots is as follows:

1. Install nodejs and npm

# download and extract wget-c https://nodejs.org/dist/v8.9.1/node-v8.9.1-linux-x64.tar.xztar-xvf node-v8.9.1-linux-x64.tar.xz# Mobile rename (optional) mv node-v8.9.1-linux-x64 / www/nodejs# create a soft connection (shortcut) If the previous step is renamed differently, the underscore part of this step will be adjusted according to the actual situation ln-s / www/nodejs/bin/node / usr/local/bin/nodeln-s / www/nodejs/bin/npm / usr/local/bin/npm

two。 Install chromium

Yum install chromium

3. Install puppeteer-core (installing pupperter directly will report an error because chromium cannot be downloaded)

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 for this step may be different from that on the Internet, one is that puppeteer-core is in require instead of puppeteer, because we are installing puppeteer-core instead of puppeteer

The other is that there are two parameters in launch. The first parameter is also because we do not install puppeteer directly, so we need to specify the path. The second parameter is due to some restrictions of chromium, which is required for root account execution.

5. Execute a.js

Node a.js

After execution, you can see an example.png.

Note: if there is Chinese on the page, it may not be displayed properly because of the lack of Chinese fonts. The solution is to upload the Chinese fonts in the c:/windows/fonts on your computer directly to the server's / usr/share/fonts/chinese (there is no chinese directory by default, just create it yourself). If not, please refer to this connection 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

Puppeter related documents https://pptr.dev/

Summary

The above is the editor to introduce to you in linux (centos) on the use of puppeteer to achieve web screenshot function, I hope to help you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report