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

一. 需求

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

二. 代码解析

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

 
  1. import time

  2. from selenium import webdriver

  3. from PIL import Image

  4. # 启动浏览器

  5. chrome_options = webdriver.ChromeOptions()

  6. # 隐藏浏览器自动化控制提示

  7. chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])

  8. # 添加谷歌浏览器驱动位置

  9. # chrome_options.binary_location = r"E:\应用\谷歌浏览器插件\chrome-win64\chrome.exe"

  10. # 加载启动项页面全屏效果,相当于F11。

  11. chrome_options.add_argument("--kiosk")

  12. # 配置初始化

  13. driver = webdriver.Chrome(options=chrome_options)

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

 
  1. # 打开页面

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

  3. driver.get(url)

  4. # 获取页面宽度与高度

  5. hight,width = 'return document.body.clientHeight','return document.body.clientWidth'

  6. h,w = driver.execute_script(hight),driver.execute_script(width)

  7. print(w,h)

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

 
  1. # 指定四个点的坐标,这里以左上角和右下角的坐标为例

  2. # x1起始宽度到 x2最大宽度的距离

  3. # y1起始高度到 y2最大高度的距离

  4. x1, y1 = 0, 0# 左上角坐标

  5. x2, y2 = w, h-600# 右下角坐标

  6. # 获取整个页面截图

  7. screenshot_path = 'screenshot.png'

  8. driver.save_screenshot(screenshot_path)

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

 
  1. # 截取指定区域

  2. im = Image.open(screenshot_path)

  3. region = im.crop((x1, y1, x2, y2))

  4. # 保存截图

  5. cropped_path = 'result_screenshot.png'

  6. region.save(cropped_path)

  7. # 关闭浏览器

  8. driver.close()

三. 完整代码

将代码中的网页链接和截取区域坐标调整为你想要的值。

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

 
  1. import time

  2. from selenium import webdriver

  3. from PIL import Image

  4. # 启动浏览器

  5. # 隐藏浏览器自动化控制提示

  6. chrome_options = webdriver.ChromeOptions()

  7. chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])

  8. # 添加谷歌浏览器驱动位置

  9. # chrome_options.binary_location = r"E:\应用\谷歌浏览器插件\chrome-win64\chrome.exe"

  10. # 加载启动项页面全屏效果,相当于F11。

  11. chrome_options.add_argument("--kiosk")

  12. # 配置初始化

  13. driver = webdriver.Chrome(options=chrome_options)

  14. # 打开页面

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

  16. driver.get(url)

  17. # 获取页面宽度与高度

  18. hight,width = 'return document.body.clientHeight','return document.body.clientWidth'

  19. h,w = driver.execute_script(hight),driver.execute_script(width)

  20. print(w,h)

  21. # 指定四个点的坐标,这里以左上角和右下角的坐标为例

  22. # x1起始宽度到 x2最大宽度的距离

  23. # y1起始高度到 y2最大高度的距离

  24. x1, y1 = 0, 0# 左上角坐标

  25. x2, y2 = w, h-600# 右下角坐标

  26. # 获取整个页面截图

  27. screenshot_path = 'screenshot.png'

  28. driver.save_screenshot(screenshot_path)

  29. time.sleep(3)

  30. # 截取指定区域

  31. im = Image.open(screenshot_path)

  32. region = im.crop((x1, y1, x2, y2))

  33. # 保存截图

  34. cropped_path = 'result_screenshot.png'

  35. region.save(cropped_path)

  36. # 关闭浏览器

  37. driver.close()

感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

 

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取   

 

  • 8
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值