2019 年 01 月 04 日 16:32:17 更新了新的 Chrome 镜像 将 Python 版本升级到了 3.7 Note: 推荐使用结尾提供的 Docker 镜像进行二次打包运行代码 各位小伙伴儿的采集日常是不是被 JavaScript 的各种点击事件折腾的欲仙欲死啊?好不容易找到个 Selenium+Chrome 可以解决问题! 但是另一个▄█▀█●的事实摆在面前,服务器都特么没有 GUI 啊・・ 好吧!咱们要知难而上!决不能被这个点小困难打倒・・・・・・・然而摆在面前的事实是・・・・他丫的各种装不上啊!坑爹啊! 那么我来拯救你们于水火之间了! 服务器如下:
1 2 3 4 5 6 7 8 9 10 11
[root@spider01 ~]# hostnamectl Static hostname: spider01 Icon name: computer-vm Chassis: vm Machine ID: 1c4029c4e7fd42498e25bb75101f85b6 Boot ID: f5a67454b94b454fae3d75ef1ccab69f Virtualization: kvm Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10 .0 -514.6 .2 .el7.x86_64 Architecture: x86-64
安装 Chromeium:
1 2 3 4
## 安装yum源 [root@spider01 ~]# sudo yum install -y epel-release ## 安装Chrome [root@spider01 ~]# yum install -y chromium
去这个地方:https://sites.google.com/a/chromium.org/chromedriver/downloads 下载 ChromeDriver 驱动放在 /usr/bin/ 目录下: 完成结果如下:
1 2 3
[root@ spider01 ~]# ll /usr/bin/ | grep chrom -rwxrwxrwx. 1 root root 7500280 11 月 29 17 :32 chromedriver lrwxrwxrwx. 1 root root 47 11 月 30 09 :35 chromium-browser -> /usr/lib64/chromium-browser/chromium-browser.sh
安装 XVFB:
1 2
[root@spider01 ~]# yum install Xvfb -y [root@spider01 ~]# yum install xorg-x11-fonts* -y
新建在 /usr/bin/ 一个名叫 xvfb-chromium 的文件写入以下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[root@spider01 ~] #!/bin/bash _kill_procs () { kill -TERM $chromium wait $chromium kill -TERM $xvfb } trap _kill_procs SIGTERMXVFB_WHD=${XVFB_WHD:-1280x720x16} Xvfb :99 -ac -screen 0 $XVFB_WHD -nolisten tcp & xvfb=$! export DISPLAY=:99chromium --no-sandbox --disable -gpu$@ & chromium=$! wait $chromium wait $xvfb
更改软连接:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
## 更改Chrome启动的软连接 [root@ spider01 ~]# ln -s /usr/lib64/chromium-browser/chromium-browser.sh /usr/bin/chromium [root@ spider01 ~]# rm -rf /usr/bin/chromium-browser [root@ spider01 ~]# ln -s /usr/bin/xvfb-chromium /usr/bin/chromium-browser [root@ spider01 ~]# ln -s /usr/bin/xvfb-chromium /usr/bin/google-chrome [root@ spider01 ~]# ll /usr/bin/ | grep chrom* -rwxrwxrwx. 1 root root 7500280 11 月 29 17 :32 chromedriver lrwxrwxrwx. 1 root root 47 11 月 30 09 :47 chromium -> /usr/lib64/chromium-browser/chromium-browser.sh lrwxrwxrwx. 1 root root 22 11 月 30 09 :48 chromium-browser -> /usr/bin/xvfb-chromium -rwxr-xr-x. 1 root root 73848 12 月 7 2016 chronyc lrwxrwxrwx. 1 root root 22 11 月 30 09 :48 google-chrome -> /usr/bin/xvfb-chromium -rwxrwxrwx. 1 root root 387 11 月 29 18 :16 xvfb-chromium
来瞅瞅能不能用哦:
1 2 3 4 5 6 7 8 9 10 11
>> > from selenium import webdriver>> > options = webdriver.ChromeOptions()>> > options.add_argument('--headless' )>> > options.add_argument('--no-sandbox' )>> > options.add_argument('--disable-extensions' )>> > options.add_argument('--disable-gpu' )>> > driver = webdriver.Chrome(options=options)>> > driver.get("http://www.baidu.com" )>> > driver.find_element_by_xpath("./*//input[@id='kw']" ).send_keys("哎哟卧槽" )>> > driver.find_element_by_xpath("./*//input[@id='su']" ).click()>> > driver.page_source
No problem!!!! 好了部署完了!当然 Docker 这么火贼适合懒人了!来来 看这儿 Docker 版的 妥妥滴!
1
docker pull thsheep/python:3.7 -debian-chrome
做好了 Python3.7 和 Chrome 集成 需要自己使用 Dockerfile 来重新打包安装你需要的 Python 包。
Note: 请按照以下方式初始化 Webdriver!!!!!!!!
1 2 3 4 5 6 7 8 9
from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument('--headless ') options.add_argument('--no -sandbox ') options.add_argument('--disable -extensions ') options.add_argument('--disable -gpu ') driver = webdriver.Chrome(options =options )
否则会出现无法初始化 Webdriver 的情况
顺便一提!!!!这个玩意儿从事 Web 测试工作的小伙伴可以用!!!!!!!!
下面是 Dockerfile 文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
FROM python:3.7-stretch ENV DBUS_SESSION_BUS_ADDRESS=/dev/null RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \ echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list && \apt-get update -qqy && \ apt-get -qqy install google-chrome-stable unzip&& \ rm /etc/apt/sources.list.d/google-chrome.list && \ rm -rf /var/lib/apt/lists/ * /var/cache/apt/ * ARG CHROME_DRIVER_VERSION=2.45 RUN wget -O /tmp/chromedriver.zip https://chromedriver.storage.googleapis.com/ $CHROME_DRIVER_VERSION/chromedriver_linux64.zip && \ rm -rf /opt/selenium/chromedriver && \ unzip /tmp/chromedriver.zip -d /opt/selenium && \ rm /tmp/chromedriver.zip && \ mv /opt/selenium/chromedriver /opt/selenium/chromedriver- $CHROME_DRIVER_VERSION && \ chmod 755 /opt/selenium/chromedriver- $CHROME_DRIVER_VERSION && \ ln -fs /opt/selenium/chromedriver- $CHROME_DRIVER_VERSION /usr/bin/chromedriver RUN google-chrome-stable --version