https://chromedriver.storage.googleapis.com/index.html 各版本各平台瀏覽器驅動下載地址(官網)
https://npm.taobao.org/mirrors/chromedriver/ 各版本各平台瀏覽器驅動下載地址(阿里鏡像)
https://www.chromedownloads.net/chrome64win/ 各版本各平台chrom下載地址
各版本驅動對應關系:
ChromeDriver Version Chrome Version
78.0.3904.11 78
77.0.3865.40 77
77.0.3865.10 77
76.0.3809.126 76
76.0.3809.68 76
76.0.3809.25 76
76.0.3809.12 76
75.0.3770.90 75
75.0.3770.8 75
74.0.3729.6 74
73.0.3683.68 73
72.0.3626.69 72
2.46 71-73
2.46 71-73
2.45 70-72
2.44 69-71
2.43 69-71
2.42 68-70
2.41 67-69
2.40 66-68
2.39 66-68
2.38 65-67
2.37 64-66
2.36 63-65
2.35 62-64
python 測試用谷歌瀏覽時,把chromedriver.exe文件放到python安裝目錄下的\Scripts下即可。
在用selenium做爬蟲的時候,需要用到谷歌或者火狐瀏覽器(CentoOS 7環境,無頭模式)
安裝谷歌瀏覽器及驅動的方式:
step1. 在官網或者鏡像網站下載chrome的rpm包,例如:google-chrome-stable_current_x86_64.rpm
step2. 下載版本對應的驅動: chromedriver_linux64.zip
step3. 安裝依賴包:
yum install -y lsb libXScrnSaver libappindicator-gtk3 liberation-fonts
step 4 安裝瀏覽器程序
rpm -ivh google-chrome-stable_current_x86_64.rpm ,如果沒有step3的那些依賴包,可能會報以下錯誤,但是根據chrome版本和平台版本不同,可能並不是都需要這些依賴包
[root@localhost src]# rpm -ivh google-chrome-stable_current_x86_64_67.0.3396.87.rpm 警告:google-chrome-stable_current_x86_64_67.0.3396.87.rpm: 頭V4 DSA/SHA1 Signature, 密鑰 ID 7fac5991: NOKEY 錯誤:依賴檢測失敗: /usr/bin/lsb_release 被 google-chrome-stable-67.0.3396.87-1.x86_64 需要 libXss.so.1()(64bit) 被 google-chrome-stable-67.0.3396.87-1.x86_64 需要 libappindicator3.so.1()(64bit) 被 google-chrome-stable-67.0.3396.87-1.x86_64 需要 xdg-utils 被 google-chrome-stable-67.0.3396.87-1.x86_64 需要
也可以在線安裝:
yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
step 5 安裝驅動【一定切記,瀏覽器和驅動的版本要匹配,驅動版本2.4和2.40是不一樣的,2.40對應v66-68的chrome】。
解壓驅動后,放在目錄 /usr/local/bin/ 下才能在代碼中自動尋找到驅動。如果放在其它自定義的目錄下,那么在代碼中需要指定驅動的具體路徑即可。
在這個目錄下執行命令
./chromedriver
可能會報錯如下:
1 [root@localhost bin]# ./chromedriver 2 ./chromedriver: error while loading shared libraries: libgconf-2.so.4: cannot open shared object file: No such file or directory
原因缺少依賴, libgconf-2.so.4的相關包
先查找哪個源包含這個命令
yum provides */libgconf-2*
輸出結果:
1 [root@localhost bin]# yum provides */libgconf-2 2 已加載插件:fastestmirror 3 Loading mirror speeds from cached hostfile 4 * base: mirrors.cn99.com 5 * extras: mirrors.cn99.com 6 * updates: mirrors.cn99.com 7 No matches found 8 [root@localhost bin]# yum provides */libgconf-2* 9 已加載插件:fastestmirror 10 Loading mirror speeds from cached hostfile 11 * base: mirrors.cn99.com 12 * extras: mirrors.cn99.com 13 * updates: mirrors.cn99.com 14 GConf2-3.2.6-8.el7.i686 : A process-transparent configuration system 15 源 :base 16 匹配來源: 17 文件名 :/usr/lib/libgconf-2.so.4.1.5 18 文件名 :/usr/lib/libgconf-2.so.4 19 20 21 22 GConf2-3.2.6-8.el7.x86_64 : A process-transparent configuration system 23 源 :base 24 匹配來源: 25 文件名 :/usr/lib64/libgconf-2.so.4 26 文件名 :/usr/lib64/libgconf-2.so.4.1.5 27 28 29 30 GConf2-devel-3.2.6-8.el7.i686 : Headers and libraries for GConf development 31 源 :base 32 匹配來源: 33 文件名 :/usr/lib/libgconf-2.so 34 35 36 37 GConf2-devel-3.2.6-8.el7.x86_64 : Headers and libraries for GConf development 38 源 :base 39 匹配來源: 40 文件名 :/usr/lib64/libgconf-2.so
從上面的輸出可以看到GConf2-devel-3.2.6-8.el7.x86_64這個package包含這個命令,安裝這個包
1 yum install GConf2-devel-3.2.6-8.el7.x86_64
安裝成功有再次運行命令,顯示版本號即安裝成功
[root@localhost bin]# ./chromedriver Starting ChromeDriver (v2.4.226074) on port 9515 #注意的是,我在安裝的時候用了v2.4.226074的版本,而瀏覽器的版本是v67,所以爬蟲代碼運行時總是報錯,最后覺悟版本應該錯了,下載v2.40的版本才可以 #以下是正確安裝后的顯示 [root@localhost src]# ./chromedriver Starting ChromeDriver 2.40.565383 (76257d1ab79276b2d53ee976b2c3e3b9f335cde7) on port 9515 Only local connections are allowed.
上述所有安裝完成后,測試安裝的瀏覽器:
google-chrome-stable --headless --disable-gpu --screenshot http://www.baidu.com/
報錯:
[1209/235648.782239:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
需要指定以no-sandbox方式運行,即允許root用戶使用瀏覽器,不指定參數的話,默認是不允許root用戶使用瀏覽器。。
google-chrome-stable --no-sandbox --headless --disable-gpu --screenshot http://www.baidu.com/
顯示以下即成功:
[1210/000036.689653:INFO:headless_shell.cc(620)] Written to file screenshot.png.
在爬蟲的python程序中:
代碼中的chrome_options.add_argument()非常關鍵,一是要以無界面形式運行,二是禁用沙盒,否則程序報錯。
# -*- coding: utf-8 -*- import time from selenium import webdriver chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--headless') # 指定無界面形式運行 chrome_options.add_argument('no-sandbox') # 禁止沙盒 driver = webdriver.Chrome(chrome_options=chrome_options) driver.get('http://www.baidu.com/') time.sleep(10) print(driver.page_source) driver.close() driver.quit()
如何卸載rpm包?
如果更新了yum源,會自動升級google瀏覽器,這時候就需要卸載被安裝的新的rpm包,重新安裝我們想要的版本
step1 先檢查google瀏覽器版本,使用命令
[root@localhost src]# google-chrome -version
Google Chrome 67.0.3396.87
step2 查找安裝的rpm包:
[root@localhost exchangeratespider]# rpm -qa | grep -i chrome google-chrome-stable-78.0.3904.108-1.x86_64
step3 卸載rmp包:
[root@localhost exchangeratespider]# rpm -e google-chrome-stable-78.0.3904.108-1.x86_64
# 再次查看,輸出為空,說明沒有了
[root@localhost exchangeratespider]# rpm -qa | grep -i chrome
# 再次安裝已經下載的rpm包
[root@localhost src]# rpm -ivh google-chrome-stable_current_x86_64_67.0.3396.87.rpm
# 查看是否安裝成功
[root@localhost src]# google-chrome -version
Google Chrome 67.0.3396.87