Linux无头浏览器搭建
Salted Fish 2025/7/18 Install
注意事项:
① 下面的过程都是在centos 7中进行的,所以选择版本是固定的。
② chrome相关的版本都是对应的118的,相关组件必须是同一版本
③ selenium 最好是选择3.0的版本。
④ 能力:代码模拟人进行页面访问点击登录的动作
# Python环境安装
# 下载包
wget https://www.python.org/ftp/python/3.8.12/Python-3.8.12.tgz
# 解压安装
tar -xzf Python-3.8.12.tgz
cd Python-3.8.12
./configure --prefix=/opt/python3
make && make install
#配置环境变量
export PATH=$PATH:/opt/python3/bin
# 创建软链接
ln -s /opt/python3/bin/python3 /usr/bin/python3
ln -s /opt/python3/bin/pip3 /usr/bin/pip3
#验证
python --version
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Selenium库及其依赖包
#需要工具
yum install python3-pip
#下载依赖包(使用清华镜像加速)
pip3 download selenium==3.141.0.0 webdriver-manager==3.7.1 \
urllib3==1.26.12 certifi==2022.12.7 \
--index-url https://pypi.tuna.tsinghua.edu.cn/simple \
-d ./offline_packages
#执行安装
pip install --no-index --find-links=./offline_packages selenium webdriver-manager
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# google-chrome-stable 安装(浏览器引擎)
#下载链接 https://pan.baidu.com/s/17M5YSKVCb516KDkE7fC88A?pwd=d48f 提取码: d48f
#安装
rpm -ivh google-chrome-stable_current_x86_64.rpm
#验证
google-chrome --version
1
2
3
4
5
6
7
2
3
4
5
6
7
# Chromium-common安装(共享库/资源文件)
#下载链接 https://pan.baidu.com/s/1FIeorOMBxS_bEW2glbFx7Q?pwd=wxey 提取码: wxey
#安装
rpm -ivh chromium-common-118.0.5993.70-1.el7.x86_64.rpm
1
2
3
4
2
3
4
# ChromeDriver安装(浏览器控制接口)
#下载链接 https://pan.baidu.com/s/13cFPiFyYKyIb4LNafRPPvA?pwd=gu7u 提取码: gu7u
#安装
rpm -ivh chromedriver-118.0.5993.70-1.el7.x86_64.rpm
#配置环境变量
export CHROMEDRIVER_PATH=/usr/local/bin/chromedriver
#权限
chmod +x /usr/local/bin/chromedriver
#验证
chromedriver --version
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 整体测试
执行面的脚本,例如:python test.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# 设置浏览器路径和驱动路径
options = Options()
options.binary_location = "/usr/bin/google-chrome"
driver = webdriver.Chrome(executable_path="/usr/local/bin/chromedriver", options=options)
# 执行操作
driver.get("https://www.baidu.com")
print(driver.title)
# 退出
driver.quit()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14