目录:index-wsl
下载安装
google chrome driver用于代码编写
https://chromedriver.storage.googleapis.com/index.html 各版本各平台浏览器驱动下载地址(官网)
https://npm.taobao.org/mirrors/chromedriver/ 各版本各平台浏览器驱动下载地址(阿里镜像)
https://www.chromedownloads.net/chrome64win/ 各版本各平台chrom下载地址
查看谷歌的版本,在地址栏输入
复制版本号,只需复制版本号最后一位小数点之前的数字。(例:版本号:85.0.4183.102
,复制85.0.4183
即可)
将复制的版本号添加在https://chromedriver.storage.googleapis.com/LATEST_RELEASE_
后面,
然后在地址栏上输入,即可知道对应的谷歌驱动号。(例:https://chromedriver.storage.googleapis.com/LATEST_RELEASE_85.0.4183
)
记住驱动号,在地址栏输入:http://chromedriver.storage.googleapis.com/index.html
,根据驱动号下载相应的驱动程序。
下载和解压驱动命令
1 sudo apt install zip unzip
1 2 3 mkdir ~/source/ cd ~/source/ sudo wget https://chromedriver.storage.googleapis.com/101.0.4951.41/chromedriver_linux64.zip
解压驱动后,创建链接到 /usr/local/bin/
下,才能在代码中自动寻找到驱动。
1 2 3 4 unzip chromedriver_linux64.zip sudo cp chromedriver /opt/chromedriver sudo ln -s /opt/chromedriver /usr/local/bin/chromedriver sudo chmod -R +rwx /usr/local/bin/chromedriver
关于安全性,见ChromeDriver - WebDriver for Chrome - Security Considerations (chromium.org)
如果放在其它自定义的目录下,那么在代码中需要指定驱动的具体路径即可。
安装依赖
在这个 /usr/local/bin/
目录下执行命令
可能会报错如下:
1 2 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的相关包
先查找哪个源包含这个命令
1 yum provides */libgconf-2*
输出结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 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
安装成功有再次运行命令,显示版本号即安装成功
1 2 3 4 5 6 7 [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.
测试程序
上述所有安装完成后,测试安装的浏览器:
需要指定以no-sandbox
方式运行,即允许root
用户使用浏览器,不指定参数的话,默认是不允许root
用户使用浏览器。。
1 google-chrome-stable --no-sandbox --headless --disable-gpu --screenshot http://www.baidu.com/
显示以下即成功:
1 [1210/000036.689653:INFO:headless_shell.cc(620)] Written to file screenshot.png.
在爬虫的python程序中:
代码中的chrome_options.add_argument()
非常关键,一是要以无界面形式运行,二是禁用沙盒,否则程序报错。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # -*- 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()
报错解决
鉴于里面的坑太多了,我单独开一篇文章做一下汇总
linux-google-chrome-and-chrome-driver-fix-it
命令行运行
如果想要在命令行使用Chrome进行测试,需要使用工具Xvfb.
X Virtual Framebuffer(Xvfb)虚拟帧缓冲器,简单来说它可以直接处理 Window的图形化功能,并且不会输出到屏幕上,这就摆脱了对可视窗口的依赖
1 2 3 4 sudo apt -y install xvfb sudo apt -y install gtk2-engines-pixbuf sudo apt -y install xfonts-cyrillic xfonts-100dpi xfonts-75dpi xfonts-base xfonts-scalable sudo apt-mark auto gtk2-engines-pixbuf xfonts-cyrillic xfonts-100dpi xfonts-75dpi xfonts-base xfonts-scalable
截图功能,可选
1 2 3 4 # 截图功能,可选 sudo apt -y install imagemagick x11-apps Xvfb -ac :99 -screen 0 1280x1024x16 & export DISPLAY=:99