Appearance
Linux 无界面服务器使用 Selenium+Chromes
0x001 安装Chrome浏览器
# 安装依赖
sudo apt-get install libxss1 libappindicator1 libindicator7
# 下载chrome包
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
# 安装chrome包
sudo dpkg -i google-chrome*.deb # Might show "errors", fixed by next line
# 修复上面的错误
sudo apt-get install -f
# 查看版本
google-chrome --no-sandbox --version
0x002 安装Selenium库
pip install selenium
0x003 下载Chrome驱动
淘宝镜像源: https://npm.taobao.org/mirrors/chromedriver
上面安装 Chrome 版本是 Google Chrome 92.0.4515.131
解压 chromedriver_linux64.zip
把解压得到的 chromedriver 文件放到 **/usr/bin ** 目录下
添加 chromedriver 可执行权限 sudo chmod +x chromedriver
0x004 测试是否成功
python
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox') # 这个配置很重要
client = webdriver.Chrome(chrome_options=chrome_options, executable_path='/home/chromedriver') # 如果没有把chromedriver加入到PATH中,就需要指明路径
client.get("https://www.baidu.com")
print(client.page_source.encode('utf-8'))
client.quit()
发现成功打印网页源码内容。
0x005 Debian安装Chrome浏览器
在一次把项目打包构建成 Dokcer 镜像的过程中,镜像选用的是 python:3.8
,使用上面方面安装 Chrome 浏览器会出现以下问题
[+] Building 17.0s (8/9)
=> [internal] load build definition from Dockerfile
=> => transferring dockerfile: 996B
=> [internal] load .dockerignore
=> => transferring context: 2B
=> [internal] load metadata for docker.io/library/python:3.8
=> [1/5] FROM docker.io/library/python:3.8@sha256:68bddbf6e88c9c88d3238e13f02edf1884fc349a0964fad4b3d44f2425791ac7
=> [internal] load build context
=> => transferring context: 102.42kB
=> CACHED [2/5] RUN apt-get update && apt-get -y install cron
=> [3/5] ADD . /opt/ad_agents_crawler
=> ERROR [4/5] RUN apt-get -y install libxss1 libappindicator1 libindicator7 && wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb && dpkg -i google-chr 1.0s
> [4/5] RUN apt-get -y install libxss1 libappindicator1 libindicator7 && wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb && dpkg -i google-chrome*.deb && apt-get install -f && google-chrome --no-sandbox --version:
#7 0.314 Reading package lists...
#7 0.753 Building dependency tree...
#7 0.878 Reading state information...
#7 0.969 E: Unable to locate package libappindicator1
#7 0.969 E: Unable to locate package libindicator7
------
executor failed running [/bin/sh -c apt-get -y install libxss1 libappindicator1 libindicator7 && wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb && dpkg -i google-chrome*.deb && apt-get install -f && google-chrome --no-sandbox --version]: exit code: 100
着重看
#7 0.969 E: Unable to locate package libappindicator1
#7 0.969 E: Unable to locate package libindicator7
这两个依赖找不到,替换源、添加源都尝试了,暂时无法成功安装,经过查找资料,找到了以下安装 Chrome 浏览器的方式,特此做个记录
# 添加密钥
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
# 添加源
sudo sh -c 'echo "deb [Arch=AMD64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
# 更新源
apt-get update
# 安装
apt-get install -y google-chrome-stable
# 查看版本
google-chrome --no-sandbox --version
Dockerfile
# Install Chrome Driver
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \
sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list' && \
apt-get update && \
apt-get install -y google-chrome-stable && \
google-chrome --no-sandbox --version