谷歌浏览器各平台各版本资源 chrome及驱动在linux中的离线安装
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