使用python录制网页

Der*_*nwa -2 python opencv python-3.x

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

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

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

谢谢。

小智 5

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

import webbrowser

webbrowser.open('http://example.com')  # Go to example.com
Run Code Online (Sandbox Code Playgroud)

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

pip3 install opencv-python numpy pyautogui
Run Code Online (Sandbox Code Playgroud)

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

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()
Run Code Online (Sandbox Code Playgroud)

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