CentOS 7使用PuppeteerSharp無頭瀏覽器注意事項


環境:

CentOS 7.6.1810

.net core 3.1

PuppeteerSharp 2.0.0

1.如網絡部穩定可以提前下載需要的chromium  

下載地址:https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/706915/chrome-linux.zip

各個系統下載地址可以查看PuppeteerSharp源碼中設置的,或者用國內鏡像

https://github.com/hardkoded/puppeteer-sharp/blob/37ea56934281209830254df3ec3ffe37c57cfac2/lib/PuppeteerSharp/BrowserFetcher.cs

將壓縮包解壓到當前程序目錄下,文件夾路徑:.local-chromium/Linux-706915/chrome-linux/

如運行報錯:加載libX11.so.6庫錯誤,則先裝該庫

Unhandled exception. System.AggregateException: One or more errors occurred. (Failed to launch Chromium! /PuppeteerTest/PuppeteerTest/.local-chromium/Linux-706915/chrome-linux/chrome: error while loading shared libraries: libX11.so.6: cannot open shared object file: No such file or directory
)
 ---> PuppeteerSharp.ChromiumProcessException: Failed to launch Chromium! /PuppeteerTest/PuppeteerTest/.local-chromium/Linux-706915/chrome-linux/chrome: error while loading shared libraries: libX11.so.6: cannot open shared object file: No such file or directory

   at PuppeteerSharp.ChromiumProcess.State.StartingState.StartCoreAsync(ChromiumProcess p)
   at PuppeteerSharp.ChromiumProcess.State.StartingState.StartCoreAsync(ChromiumProcess p)
   at PuppeteerSharp.Launcher.LaunchAsync(LaunchOptions options)
   at PuppeteerSharp.Launcher.LaunchAsync(LaunchOptions options)
   --- End of inner exception stack trace ---

從pkgs網站找到該庫 https://pkgs.org/download/libX11.so.6

 

進入該庫

https://centos.pkgs.org/7/centos-x86_64/libX11-1.6.7-2.el7.i686.rpm.html

找到yum安裝命令:

Install libX11 rpm package:

# yum install libX11

 

 

 

 在Xshell執行該命令

 

 

 

 

 

其他庫報錯,如libXcomposite庫,則一樣到pkgs網站查找庫和安裝命令

Unhandled exception. System.AggregateException: One or more errors occurred. (Failed to launch Chromium! /PuppeteerTest/PuppeteerTest/bin/Debug/netcoreapp3.1/.local-chromium/Linux-706915/chrome-linux/chrome: error while loading shared libraries: libXcomposite.so.1: cannot open shared object file: No such file or directory
)
 ---> PuppeteerSharp.ChromiumProcessException: Failed to launch Chromium! /PuppeteerTest/PuppeteerTest/bin/Debug/netcoreapp3.1/.local-chromium/Linux-706915/chrome-linux/chrome: error while loading shared libraries: libXcomposite.so.1: cannot open shared object file: No such file or directory

   at PuppeteerSharp.ChromiumProcess.State.StartingState.StartCoreAsync(ChromiumProcess p)
   at PuppeteerSharp.ChromiumProcess.State.StartingState.StartCoreAsync(ChromiumProcess p)
   at PuppeteerSharp.Launcher.LaunchAsync(LaunchOptions options)
   at PuppeteerSharp.Launcher.LaunchAsync(LaunchOptions options)
   --- End of inner exception stack trace ---

https://centos.pkgs.org/

 

 

 

 其他庫報錯安裝方法一樣。

報libXss.so.1這個庫錯誤的時候,運行命令:

yum install libXss* -y

報libatk-1.0.so庫錯誤運行命令:

yum install atk

報libatk-bridge-2.0.so庫錯誤運行命令:

yum install at-spi2-atk-devel

報libpangocairo-1.0.so庫錯誤運行命令:

yum install pango-devel

報libgtk-3.so庫錯誤運行命令:

yum install gtk3-devel

   

 Pupperteer官網可以查到CentOS的相關依賴

 

 

 https://github.com/puppeteer/puppeteer/blob/master/docs/troubleshooting.md

 

 

 

全部依賴都安裝好后,運行還是報錯:--no-sandbox

Unhandled exception. System.AggregateException: One or more errors occurred. (Failed to launch Chromium! [0416/165456.543755:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
)
 ---> PuppeteerSharp.ChromiumProcessException: Failed to launch Chromium! [0416/165456.543755:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.

   at PuppeteerSharp.ChromiumProcess.State.StartingState.StartCoreAsync(ChromiumProcess p)
   at PuppeteerSharp.ChromiumProcess.State.StartingState.StartCoreAsync(ChromiumProcess p)
   at PuppeteerSharp.Launcher.LaunchAsync(LaunchOptions options)
   at PuppeteerSharp.Launcher.LaunchAsync(LaunchOptions options)
   --- End of inner exception stack trace ---

根據網上的一篇文章,啟動的時候需要加上--no-sandbox參數

https://segmentfault.com/a/1190000018553178

var launchOptions = new LaunchOptions
                {
                    Headless = true
                };
                launchOptions.Args = new string[] {
                    "--no-sandbox"
                };
                var    browser = Puppeteer.LaunchAsync(launchOptions).Result;

 重新啟動下程序,已經可以抓取網頁了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM