Selenium 自动化截取网页指定区域截图

一. 需求

在本篇博客中,我将分享一段使用Python编写的自动化网页截图代码,该代码基于Selenium和PIL库,可用于截取网页中指定区域的截图。这样的功能对于需要定期监控特定网页内容或进行网页数据采集的任务非常有用。

二. 代码解析

首先,我们使用Selenium库启动了一个Chrome浏览器,并通过设置选项隐藏了浏览器自动化控制提示。代码还实现了全屏效果,类似于按下F11键。

python 复制代码
import time
from selenium import webdriver
from PIL import Image

# 启动浏览器
chrome_options = webdriver.ChromeOptions()
# 隐藏浏览器自动化控制提示
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])

# 添加谷歌浏览器驱动位置
# chrome_options.binary_location = r"E:\应用\谷歌浏览器插件\chrome-win64\chrome.exe"

# 加载启动项页面全屏效果,相当于F11。
chrome_options.add_argument("--kiosk")

# 配置初始化
driver = webdriver.Chrome(options=chrome_options)

然后,我们打开了指定的网页,并获取了页面的宽度和高度。

python 复制代码
# 打开页面
url = 'https://baidu.com'  # 请替换为你要打开的页面
driver.get(url)

# 获取页面宽度与高度
hight,width = 'return document.body.clientHeight','return document.body.clientWidth'
h,w = driver.execute_script(hight),driver.execute_script(width)
print(w,h)

接下来,我们指定了要截取的区域的坐标,并使用Selenium的save_screenshot方法保存整个页面的截图。

python 复制代码
# 指定四个点的坐标,这里以左上角和右下角的坐标为例
# x1起始宽度到 x2最大宽度的距离
# y1起始高度到 y2最大高度的距离
x1, y1 = 0, 0  # 左上角坐标
x2, y2 = w, h-600  # 右下角坐标

# 获取整个页面截图
screenshot_path = 'screenshot.png'
driver.save_screenshot(screenshot_path)

随后,我们使用PIL库打开整个页面的截图,并通过crop方法截取指定区域。最后,保存截取的区域截图及关闭浏览器

python 复制代码
# 截取指定区域
im = Image.open(screenshot_path)
region = im.crop((x1, y1, x2, y2))

# 保存截图
cropped_path = 'result_screenshot.png'
region.save(cropped_path)

# 关闭浏览器
driver.close()

三. 总结

1. 完整代码

python 复制代码
import time

from selenium import webdriver
from PIL import Image

# 启动浏览器
# 隐藏浏览器自动化控制提示
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])

# 添加谷歌浏览器驱动位置
# chrome_options.binary_location = r"E:\应用\谷歌浏览器插件\chrome-win64\chrome.exe"

# 加载启动项页面全屏效果,相当于F11。
chrome_options.add_argument("--kiosk")

# 配置初始化
driver = webdriver.Chrome(options=chrome_options)

# 打开页面
url = 'https://baidu.com'  # 请替换为你要打开的页面
driver.get(url)


# 获取页面宽度与高度
hight,width = 'return document.body.clientHeight','return document.body.clientWidth'
h,w = driver.execute_script(hight),driver.execute_script(width)
print(w,h)

# 指定四个点的坐标,这里以左上角和右下角的坐标为例
# x1起始宽度到 x2最大宽度的距离
# y1起始高度到 y2最大高度的距离
x1, y1 = 0, 0  # 左上角坐标
x2, y2 = w, h-600  # 右下角坐标

# 获取整个页面截图
screenshot_path = 'screenshot.png'
driver.save_screenshot(screenshot_path)

time.sleep(3)

# 截取指定区域
im = Image.open(screenshot_path)
region = im.crop((x1, y1, x2, y2))

# 保存截图
cropped_path = 'result_screenshot.png'
region.save(cropped_path)

# 关闭浏览器
driver.close()
  • 将代码中的网页链接和截取区域坐标调整为你想要的值。

  • 运行代码,即可生成指定区域的网页截图。

相关推荐
call_me_wangcheng6 分钟前
django创建项目
python·django
VXbishe23 分钟前
(附源码)基于springboot的“我来找房”微信小程序的设计与实现-计算机毕设 23157
java·python·微信小程序·node.js·c#·php·课程设计
chusheng184025 分钟前
Python 如何使用 multiprocessing 模块创建进程池
python·信息可视化·matplotlib
猿小猴子27 分钟前
Python3 爬虫 中间人爬虫
开发语言·爬虫·python
子墨7771 小时前
Flask之Hello world 详解
python
繁依Fanyi1 小时前
旅游心动盲盒:开启个性化旅行新体验
java·服务器·python·算法·eclipse·tomcat·旅游
计算机编程-吉哥1 小时前
计算机毕业设计 基于Python的个性化旅游线路推荐系统的设计与实现 Python+Django+Vue 前后端分离 附源码 讲解 文档
python·django·毕业设计·课程设计·毕业论文·计算机毕业设计选题·个性化旅游线路推荐系统
罔闻_spider2 小时前
爬虫prc技术----小红书爬取解决xs
爬虫·python·算法·机器学习·自然语言处理·中文分词
python机器学习ML2 小时前
机器学习K近邻算法——python详细代码解析(sklearn)(1)
python·机器学习·近邻算法·knn
DanCheng-studio2 小时前
毕设 大数据抖音短视频数据分析与可视化(源码)
python·毕业设计·毕设