CentOS7下python3 selenium3 使用Chrome的无头浏览器 截取网页全屏图片

Andy 2022年07月05日 657次浏览

前言

selenium是一个模拟浏览器的自动化执行框架,但是如果每次执行都要打开浏览器来处理任务的话,效率上都不高。最重要的是如果安装在Centos7服务器环境下,打开浏览器来模拟操作是更加不合适的,尤其是碰上需要截取网页图片这样的需求。

这时候就要考虑使用Chrome的无头浏览器模式了。所谓的无头浏览器模式也就是不需要打开浏览器,但是却可以起到模拟打开浏览器的执行效果,一切无界面执行。

下面来看看如果安装部署到执行。

1.安装 chrome

1.1 添加 google的 repo源

vi /etc/yum.repos.d/google.repo
在打开的空文件中填入以下内容

[google]
name=Google-x86_64
baseurl=http://dl.google.com/linux/rpm/stable/x86_64
enabled=1
gpgcheck=0
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub

1.2 使用 yum安装 chrome浏览器

$ sudo yum makecache
$ sudo yum install google-chrome-stable -y

2.安装 chromedriver驱动

2.1 查看 chrome的版本

安装成功之后,查看安装的chrom版本如下:

[root@locust03 ~]# google-chrome --version
Google Chrome 103.0.5060.114

2.2 下载 chromedriver

selenium如果想要执行 chrome浏览器的话,是需要安装驱动 chromedriver的,而下载 chromedriver可以从两个地方去下载,点击访问如下:

点击访问官网
点位访问国内淘宝镜像地址
https://registry.npmmirror.com/binary.html?path=chromedriver/
那么其实一般都是访问国内的镜像地址,

可以看到提供下载的版本挺多的,从上面看到刚刚安装的chrome版本号 Google Chrome 103.0.5060.114 ,所以按照版本号大概搜索一下,

2.3 添加至环境变量 $PATH

我将 chromedriver_linux64.zip下载在 /opt目录下,然后进行解压。最后写入环境配置文件 /etc/profile即可。

1.进入opt目录
[root@server opt]# cd /opt/

2.下载chromdirver
[root@server opt]# wget http://npm.taobao.org/mirrors/chromedriver/103.0.5060.53/chromedriver_linux64.zip

3.解压zip包
[root@server opt]# unzip chromedriver_linux64.zip

4.得到一个二进制可执行文件
[root@server opt]# ls -ll chromedriver
-rwxrwxr-x 1 root root 11610824 Nov 19 02:20 chromedriver

5.创建存放驱动的文件夹driver
[root@server opt]# mkdir -p /opt/driver/bin

6.将chromedirver放入文件夹driver中bin下
[root@server opt]# mv chromedriver /opt/driver/bin/
配置环境变量如下:
[root@server driver]# vi /etc/profile

添加内容

export DRIVER=/opt/driver
export PATH=$PATH:$DRIVER/bin

设置环境变量立即生效,并执行全局命令查看chromedirver版本:

[root@server ~]# source /etc/profile
[root@server ~]#
[root@server ~]# chromedriver --version
ChromeDriver 103.0.5060.53
[root@server ~]#

能全局执行 chromedriver说明环境配置生效了。

  1. 安装 selenium

yum install -y python3-pip

selenium可以在你项目的虚拟环境中简单地用 pip安装

pip3 install selenium
执行查看安装的版本如下:

[root@server selenium_ex]# pip3 install selenium
Looking in indexes: http://mirrors.tencentyun.com/pypi/simple
Collecting selenium
Downloading http://mirrors.tencentyun.com/pypi/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl (904kB)
|████████████████████████████████| 911kB 990kB/s
Requirement already satisfied: urllib3 in /usr/local/python3/lib/python3.7/site-packages (from selenium) (1.25.6)
Installing collected packages: selenium
Successfully installed selenium-3.141.0
[root@locust03 selenium_ex]#

  1. 脚本测试

编写一个test.py的脚本,如下:

from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
import time
import os.path

#配置驱动路径
DRIVER_PATH = '/opt/driver/bin/chromedriver'

if __name__ == "__main__":
    # 设置浏览器
    options = Options()
    options.add_argument('--no-sandbox')
    options.add_argument('--headless')  # 无头参数
    options.add_argument('--disable-gpu')
    # 启动浏览器
    driver = Chrome(executable_path=DRIVER_PATH, options=options)
    driver.maximize_window()

    try:
        # 访问页面
        url = 'https://www.baidu.com'
        driver.get(url)
        time.sleep(1)

        # 设置截屏整个网页的宽度以及高度
        scroll_width = 1600
        scroll_height = 1500
        driver.set_window_size(scroll_width, scroll_height)

        # 保存图片
        #img_path = os.getcwd()
        img_path = '/opt'
        img_name = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))
        img = "%s.png" % os.path.join(img_path, img_name)
        driver.get_screenshot_as_file(img)

        # 关闭浏览器
        driver.close()
        driver.quit()

    except Exception as e:
        print(e)

在服务器上执行如下:

[root@server selenium_ex]# python3 test.py
[root@server selenium_ex]#
[root@server selenium_ex]# ls
2019-11-28-15-06-48.png test.py
[root@server selenium_ex]#

已经能够正常模拟浏览器登陆,并且截取网页的图片下来。可以从图片中看到,凡是中文的地方都是显示方框的符号,这是因为Centos7默认下是没有安装中文字体的,所以chrom浏览器打开就无法正常显示中文。