CentOs安装chrome

本文详细介绍了如何在CentOS7系统中安装谷歌浏览器GoogleChrome及其驱动chromedriver,包括添加Yum源,安装浏览器,下载并配置驱动,解决环境变量问题,以及使用Python的Selenium库进行测试。
摘要由CSDN通过智能技术生成

前言

爬虫免不了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()

大功告成

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值