使用 python 录制网页

record a web page using python

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

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

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

谢谢。

要打开特定的 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()

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