【轉載請注明出處】: https://blog.csdn.net/huahao1989/article/details/107890747 Chrome官方網站已經說的很清楚,不再支援6.x的CentOS,至少7以上。 可是很多時候我們使用的伺服器版本并不能随便更新,即便已經很難受了,但是還得繼續使用低版本,裝起來那是真叫一個費勁,還好就是費勁一些,最終還是可以裝成功的。
什麼是 Headless Chrome
Headless Chrome 是 Chrome 浏覽器的無界面形态,可以在不打開浏覽器的前提下,使用所有 Chrome 支援的特性運作你的程式。相比于現代浏覽器,Headless Chrome 更加友善測試 web 應用,獲得網站的截圖,做爬蟲抓取資訊等。相比于出道較早的 PhantomJS,SlimerJS 等,Headless Chrome 則更加貼近浏覽器環境。
CentOS版本
lsb_release -a
最新版本Google-Chrome安裝
CentOS/RedHat 7以上安裝google-chrome可以完全參考
https://intoli.com/blog/installing-google-chrome-on-centos/(6及以下版本不适用)。
指定yum源
伺服器應該指定合适yum源,避免找不到某些依賴的尴尬。
修改 /etc/yum.repos.d/CentOS-Base.repo,可以使用阿裡的yum repo:
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
yum clean all
安裝google-chrome
根據
,執行指令:
curl https://intoli.com/install-google-chrome.sh | bash
腳本會自動檢測目前版本安裝chrome所缺失的依賴包并下載下傳。
檢查是否還缺乏依賴:
ldd /opt/google/chrome/chrome | grep "not found"
傳回為空,說明CentOS下chrome依賴問題基本解決。
運作chrome
執行
google-chrome-stable --no-sandbox --headless --disable-gpu --screenshot https://www.suning.com/。
通路成功,在目前目錄會生成截圖screenshot.png,如果報錯
[0100/000000.311368:ERROR:broker_posix.cc(43)] Invalid node channel message
則需要安裝依賴包:
yum install \
ipa-gothic-fonts \
xorg-x11-fonts-100dpi \
xorg-x11-fonts-75dpi \
xorg-x11-utils \
xorg-x11-fonts-cyrillic \
xorg-x11-fonts-Type1 \
xorg-x11-fonts-misc -y
最新版本Chromedriver安裝
目前的chrome版本是 google-chrome-stable-72.0.3626.109-1.x86_64,chromedrive的官網是
https://sites.google.com/a/chromium.org/chromedriver/downloads下載下傳位址
https://chromedriver.storage.googleapis.com/2.46/chromedriver_linux64.zip或者選擇taobao鏡像下載下傳
http://npm.taobao.org/mirrors/chromedriver/鏡像下載下傳位址
http://npm.taobao.org/mirrors/chromedriver/2.46/chromedriver_linux64.zip解壓後部署到/opt/drivers目錄下,嘗試運作:
./chromedriver
Starting ChromeDriver 72.0.3626.7 (efcef9a3ecda02b2132af215116a03852d08b9cb) on port 9515
Only local connections are allowed.
[1550143530.011][SEVERE]: CreatePlatformSocket() returned an error, errno=0: Address family not supported by protocol (97)
另外要修改/etc/hosts,綁定127.0.0.1 localhost,否則,Java Selenium運作時chromedriver可能因為找不到localhost報逾時異常
安裝selenium
-
安裝 Python 并配置好環境變量
shell輸入:
出現對應版本号即安裝成功!python -V
- 安裝 pip
python預設自帶 pip 在安裝目錄的scripts目錄下,自行配置至環境變量即可,配置好後shell輸入:`pip -V` 出現對應版本号即安裝成功!
- 安裝 selenium
shell輸入:`pip install selenium` 提示:Successfully installed selenium-即安裝成功!
python
>>>from selenium import webdriver
>>>driver = webdriver.Chrome()
>>>driver.get('https://www.baidu.com')
已經可以了,正常寫python腳本即可。
搭建環境時遇到的問題
1、/lib64/libc.so.6: version `GLIBC_2.14' not found (required by ./chromedriver)
#檢視系統版本
cat /etc/redhat-release
#檢視glibc支援的版本
strings /lib64/libc.so.6 |grep GLIBC_
wget http://ftp.gnu.org/gnu/glibc/glibc-2.14.tar.gz
wget http://ftp.gnu.org/gnu/glibc/glibc-ports-2.14.tar.gz
tar -xvf glibc-2.14.tar.gz
tar -xvf glibc-ports-2.14.tar.gz
mv glibc-ports-2.14 glibc-2.14/ports
mkdir glibc-2.14/build
cd glibc-2.14/build
../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
make
make install
安裝編譯過程中需要注意三點:
- 要将glibc-ports解壓到glibc目錄下
- 不能在glibc目前目錄下運作configure
- 加上優化開關,export CFLAGS="-g -O2 -march=i486",否則會出現錯誤
- 在make install過程中可能會出現 nss_test1加載不了的情況, 此時可以将加載libnss_test1.so.2的地方注釋掉,用
指令在 /glibc目錄下查找一下,加載的地方也不多(這隻是一個測試nss的靜态庫,可以不要)grep "nss_test1" . -nr
2、/lib64/libc.so.6: version `GLIBC_2.16' not found (required by ./chromedriver)
wget http://ftp.gnu.org/gnu/glibc/glibc-2.16.0.tar.gz
wget http://ftp.gnu.org/gnu/glibc/glibc-ports-2.16.0.tar.gz
tar -xvf glibc-2.16.0.tar.gz
tar -xvf glibc-ports-2.16.0.tar.gz
mv glibc-ports-2.16.0 glibc-2.16.0/ports
mkdir glibc-2.16.0/build
cd glibc-2.16.0/build
../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
make
make install
報錯
Unmatched ( in regex; marked by HERE in m/$( <-- HERE if $(abi-64-ld-soname),$(abi-64-ld-soname),ld/ at scripts/test-installation.pl line
可以參考解決辦法
https://sourceware.org/bugzilla/attachment.cgi?id=6616&action=diff- glibc-2.16.0/Makefile
ifeq (,$(install_root))
CC="$(CC)" $(PERL) scripts/test-installation.pl $(common-objpfx)
endif
改成
ifeq (,$(install_root))
LD_SO=$(ld.so-version) CC="$(CC)" $(PERL) scripts/test-installation.pl $(common-objpfx)
endif
- glibc-2.16.0/scripts/test-installation.pl
sub usage {
print "Usage: test-installation [soversions.mk]\n";
上面添加
if ($ENV{LD_SO}) {
$LD_SO = $ENV{LD_SO};
} else {
$LD_SO= "";
}
sub usage {
print "Usage: test-installation [soversions.mk]\n";
在
} else {
if (/^ld\.so/) {
($ld_so_name, $ld_so_version)= /=(.*)\.so\.(.*)$/;
} elsif ($LD_SO ne "") {
($ld_so_name, $ld_so_version) = split ('\.so\.', $LD_SO);
} else {
if (/^ld\.so/) {
($ld_so_name, $ld_so_version)= /=(.*)\.so\.(.*)$/;
歡迎關注 “後端老鳥” 公衆号,接下來會發一系列的專題文章,包括Java、Python、Linux、SpringBoot、SpringCloud、Dubbo、算法、技術團隊的管理等,還有各種腦圖和學習資料,NFC技術、搜尋技術、爬蟲技術、推薦技術、音視訊互動直播等,隻要有時間我就會整理分享,敬請期待,現成的筆記、腦圖和學習資料如果大家有需求也可以公衆号留言提前擷取。由于本人在所有團隊中基本都處于攻堅和探路的角色,搞過的東西多,遇到的坑多,解決的問題也很多,歡迎大家加公衆号進群一起交流學習。