使用python录制网页

德里克·阿曼瓦

我想尝试在 python 中创建一个程序,但我不知道如何开始。我希望程序执行以下操作:

  1. 转到特定的网站 URL
  2. 对显示的整个网页进行 30 秒的录制或视频。
  3. 将录制内容保存在视频文件中。

是否有可能实现这一点,我将在 python 中使用哪些库来创建一个执行此操作的程序?我有一个想法,opencv-python 可能是我可以使用的库之一,这可能吗?

谢谢。

grosklappe2

要打开特定的 URL,您可以使用模块“webbrowser”:

import webbrowser

webbrowser.open('http://example.com')  # Go to example.com

为了记录页面,您可以安装模块“opencv-python”、“numpy”和“pyautogui”:

pip3 install opencv-python numpy pyautogui

然后使用它们来获得最终代码,它可能如下所示:

import cv2
import numpy as np
import os
import pyautogui
import webbrowser

webbrowser.open('http://example.com')  # Go to example.com

output = "video.avi"
img = pyautogui.screenshot()
img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
#get info from img
height, width, channels = img.shape
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output, fourcc, 20.0, (width, height))

for i in range(95):
    try:
        img = pyautogui.screenshot()
        image = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
        out.write(image)
        StopIteration(0.5)
    except KeyboardInterrupt:
        break

print("finished")
out.release()
cv2.destroyAllWindows()

该文件应另存为“视频”并且可以运行。在屏幕截图期间应自动检测您的屏幕分辨率。如果此代码中有任何问题,请随时告诉我。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章