前言
爬虫免不了selenium,selenium免不了谷歌,centos安装谷歌浏览器及驱动,来了。
安装chrome
1.系统需求
CentOS 7 系统必须是 64 位系统,Chrome 没有 32 位系统的软件包。
2.在 CentOS 7 系统中添加谷歌官方 Yum 软件源
在 CentOS 7 系统下的 /etc/yum.repos.d/ 文件夹中创建 google-chrome.repo 文件:
sudo vi /etc/yum.repos.d/google-chrome.repo
添加如下 Yum 软件源信息:
[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch
enabled=1
gpgcheck=0
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
3.查看谷歌 Chrome 浏览器的软件包信息
sudo yum info google-chrome-stable
系统可能会显示如下信息:
可安装的软件包
名称 :google-chrome-stable
架构 :x86_64
版本 :67.0.3396.87
发布 :1
大小 :50 M
源 :google-chrome/x86_64
简介 : Google Chrome
网址 :https://chrome.google.com/
协议 : Multiple, see https://chrome.google.com/
描述 : The web browser from Google
:
: Google Chrome is a browser that combines a minimal design with
: sophisticated technology to make the web faster, safer, and easier.
从显示信息上可以看出当前的 Chrome 版本是 67。
4.在 CentOS 7 系统中安装 Chrome 浏览器稳定版
sudo yum install google-chrome-stable
5.检查版本
[root@localhost ~]# google-chrome-stable --version
Google Chrome 109.0.5414.119
安装驱动
根据上面检查的版本,去这个网页https://npm.taobao.org/mirrors/chromedriver/找到对应的路径,找到linux 版,右键-复制链接
wget 复制的链接
unzip chromedriver_linux64.zip
然后执行命令
chromedriver
会发现报错
-bash: /usr/bin/chromedriver: 没有那个文件或目录
这是因为没有添加进环境变量的原因,把路径添加到PATH下,再次执行
[root@localhost ~]# chromedriver
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
安装成功,然后
pip install selenium
测试程序
from selenium import webdriver as wdr
from selenium.webdriver .chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--headless')
dr = wdr.Chrome(chrome_options=chrome_options)
dr.get('http://www.baidu.com/')
print(dr.title)
dr.quit()
大功告成