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

前言

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

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

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

1.安装chrome

1.1 添加googlerepo

vim /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@localhost opt]# google-chrome --version
Google Chrome 79.0.3945.79
[root@localhost opt]#

 

2.2 下载chromedriver

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

那么其实一般都是访问国内的镜像地址,如下:

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

点击最新的版本号进去,可以看到下载的系统版本,

因为我准备安装在Centos7服务器上,所以选择linux64位的版本。

wget http://npm.taobao.org/mirrors/chromedriver/79.0.3945.79/chromedriver_linux64.zip

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

# 1.进入opt目录
[root@localhost opt]# cd /opt/
# 2.下载chromdirver
[root@localhost opt]# wget http://npm.taobao.org/mirrors/chromedriver/78.0.3904.105/chromedriver_linux64.zip
# 3.解压zip包
[root@localhostopt]# 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@localhost opt]# mkdir -p /opt/driver/bin
# 6.将chromedirver放入文件夹driver中bin下
[root@localhost opt]# mv chromedriver /opt/driver/bin/

  配置环境变量如下:

[root@localhost driver]# vim /etc/profile
...
# 添加内容
export DRIVER=/opt/driver
export PATH=$PATH:$DRIVER/bin

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

[root@localhost  ~]# source /etc/profile
[root@localhost ~]# 
[root@localhost ~]# chromedriver --version
ChromeDriver 78.0.3904.105 (60e2d8774a8151efa6a00b1f358371b1e0e07ee2-refs/branch-heads/3904@{#877})
[root@localhost ~]# 

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

3. 安装selenium

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

pip3 install selenium

4. 脚本测试

编写一个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.cnblogs.com/llxpbbs/'
        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_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@localhost opt]# python3 test.py
[root@localhost opt]# ll
总用量 4812
-rw-r--r-- 1 root root   44991 8月  22 18:19 2019-12-17-18-19-32.png
-rw-r--r-- 1 root root 4875160 11月 19 2019 chromedriver_linux64.zip
drwxr-xr-x 3 root root      17 8月  22 17:34 driver
drwxr-xr-x 3 root root      20 8月  22 17:29 google
-rw-r--r-- 1 root root    1158 12月 17 2019 test.py
[root@localhost opt]#

  将图片下载查看一下即可,

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

解决的方式只要给Centos7安装中文字体即可,这里就不多做介绍。

猜你喜欢

转载自www.cnblogs.com/llxpbbs/p/12053773.html
今日推荐