Centos8 chrome+selenium+chromedriver

Centos8 chrome+selenium+chromedriver,第1张

Centos8 chrome+selenium+chromedriver 实验环境

阿里云服务器 centos 8.3

实验步骤

查看Linux虚拟机内核版本信息

cat  /etc/redhat-release

安装sudo

yum install sudo
yum换源

我在使用阿里云默认设置时,有多个包yum install package都match 不到,在换源后就可以找到了
参考链接 Centos7配置yum源

chrome安装

安装方式因下载软件包格式不同而不同,以deb和rpm为例,建议直接下载rpm格式软件包进行安装。

deb格式 alien安装

如果你下载的安装包是rpm格式可以跳过此步
在alien网站找到你需要的版本

yum -y install rpm-build
yum -y install python-libs python-wnck
wget http://ftp.de.debian.org/debian/pool/main/a/alien/alien_8.92.tar.gz
tar zxvf alien_8.92.tar.gz
cd alien
perl Makefile.PL
make
make install
alien -r your_file.deb
rpm -ivh your_file.rpm
rpm格式
## 建议直接按以下方式安装chrome
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm --no-check-certificate  
sudo yum install google-chrome-stable_current_x86_64.rpm
selenium安装
pip3 install selenium
chromedriver安装
# 查看chrome版本号
google-chrome --version

在chromedriver页面找到和chrome版本一致的chromedriver

wget http://chromedriver.storage.googleapis.com/96.0.4664.45/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
mv chromedriver /usr/bin # 解压后移动到该目录
chmod 777 chromedriver # 这个我看到别的教程有加的
测试

编写一个 test.py 文件

# test.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
 
chrome_options = Options()
chrome_options.add_argument('--headless') # 16年之后,chrome给出的解决办法,抢了PhantomJS饭碗
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')  # root用户不加这条会无法运行
 
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("https://www.baidu.com/")
print(driver.title)
driver.close()
# 正常输出为"百度一下,你就知道"

命令行运行

python test.py

使用云服务器运行爬虫时,若想要关闭命令行窗口程序继续运行可以使用nohup命令。

# 在后台运行 your_file.py 的python脚本
nohup python your_file.py &
# 查看后台python运行状态 
ps -ef | grep python

参考链接
CentOS 如何将.deb 文件 转换.rpm
CentOS + Selenium+Chrome环境安装
linux(centos)查看chrome版本号
Centos7安装Selenium+chrome+chromedriver详细
centos7 安装selenium和chrome和chrome-driver
nohup大法让程序在服务器后台运行

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5694052.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存