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

前言

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

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

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

1.安装chrome

1.1 添加google的repo源

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安装中文字体即可,这里就不多做介绍。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你在 CentOS 7 上使用 SeleniumChrome,且设置 `download.default_directory` 不生效,可能是因为 Chrome 在 headless 模式下没有权限访问文件系统。你可以尝试以下解决方案: 1. 给 Chrome 添加用户组权限 执行以下命令,将当前用户添加到 `chrome` 用户组中: ``` sudo usermod -a -G chrome $USER ``` 然后重新启动 Chrome,试试能否下载文件。 2. 设置 Chrome 的命令行参数 在启动 Chrome 时,通过命令行参数来设置 `download.default_directory`。例如: ```python from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument('--disable-dev-shm-usage') options.add_argument('--no-sandbox') options.add_argument('--headless') options.add_argument('--disable-gpu') options.add_argument('--disable-browser-side-navigation') options.add_argument('--disable-infobars') options.add_argument('--disable-popup-blocking') options.add_argument('--start-maximized') options.add_argument('--window-size=1920,1080') options.add_argument('--disable-setuid-sandbox') options.add_argument('--disable-extensions') options.add_argument('--disable-features=VizDisplayCompositor') options.add_argument('--disable-features=NetworkService') options.add_argument('--disable-features=VizHitTestSurfaceLayer') options.add_argument('--disable-features=TaskSchedulerFromIdle') options.add_argument('--disable-features=RendererCodeIntegrity') options.add_argument('--disable-features=AudioServiceOutOfProcess') options.add_argument('--disable-features=VizDisplayCompositor') options.add_argument('--disable-features=OutOfBlinkCors') options.add_argument('--disable-features=RendererCodeIntegrityVerification') options.add_argument('--disable-features=RendererCodeIntegrity') options.add_argument('--disable-features=AudioServiceOutOfProcess') options.add_argument('--disable-features=VizHitTestSurfaceLayer') options.add_argument('--disable-features=VizHitTestDrawQuad') options.add_argument('--disable-features=VizHitTestDrawQuad') options.add_argument('--disable-features=NetworkService') options.add_argument('--disable-features=TaskSchedulerFromIdle') options.add_argument('--disable-features=OutOfBlinkCors') options.add_argument('--disable-features=RendererCodeIntegrityVerification') options.add_argument('--disable-features=RendererCodeIntegrity') options.add_argument('--disable-features=NetworkServiceInProcess') options.add_argument('--disable-features=VizDisplayCompositor') options.add_argument('--disable-features=VizHitTestSurfaceLayer') options.add_argument('--disable-features=VizHitTestDrawQuad') options.add_argument('--disable-features=VizHitTestDrawQuad') options.add_argument('--disable-features=TaskSchedulerFromIdle') options.add_argument('--disable-features=RendererCodeIntegrityVerification') options.add_argument('--disable-features=RendererCodeIntegrity') options.add_argument('--disable-features=NetworkService') options.add_argument('--disable-features=OutOfBlinkCors') options.add_argument('--disable-features=VizHitTestSurfaceLayer') options.add_argument('--disable-features=NetworkServiceInProcess') options.add_argument('--disable-features=VizDisplayCompositor') options.add_argument('--disable-features=VizHitTestDrawQuad') options.add_argument('--disable-features=RendererCodeIntegrityVerification') options.add_argument('--disable-features=RendererCodeIntegrity') options.add_argument('--disable-features=VizHitTestDrawQuad') options.add_argument('--disable-features=NetworkService') options.add_argument('--disable-features=TaskSchedulerFromIdle') options.add_argument('--disable-features=OutOfBlinkCors') options.add_argument('--disable-features=RendererCodeIntegrityVerification') options.add_argument('--disable-features=RendererCodeIntegrity') options.add_argument('--disable-features=AudioServiceOutOfProcess') options.add_argument('--disable-features=VizDisplayCompositor') options.add_argument('--disable-features=VizHitTestSurfaceLayer') options.add_argument('--disable-features=NetworkServiceInProcess') options.add_argument('--disable-features=VizHitTestDrawQuad') options.add_argument('--disable-features=VizHitTestDrawQuad') options.add_argument('--disable-features=RendererCodeIntegrityVerification') options.add_argument('--disable-features=RendererCodeIntegrity') options.add_argument('--disable-features=NetworkService') options.add_argument('--disable-features=TaskSchedulerFromIdle') options.add_argument('--disable-features=OutOfBlinkCors') options.add_argument('--disable-features=VizHitTestSurfaceLayer') options.add_argument('--disable-features=RendererCodeIntegrityVerification') options.add_argument('--disable-features=RendererCodeIntegrity') options.add_argument('--disable-features=VizHitTestDrawQuad') options.add_argument('--disable-features=NetworkService') options.add_argument('--disable-features=TaskSchedulerFromIdle') options.add_argument('--disable-features=OutOfBlinkCors') options.add_argument('--disable-features=VizHitTestSurfaceLayer') options.add_argument('--disable-features=RendererCodeIntegrityVerification') options.add_argument('--disable-features=RendererCodeIntegrity') options.add_argument('--disable-features=VizHitTestDrawQuad') options.add_argument('download.default_directory=/path/to/download/directory') driver = webdriver.Chrome(options=options) ``` 其中,`/path/to/download/directory` 为你要下载文件的目录路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值