【问题标题】:record a web page using python [closed]使用python记录网页[关闭]
【发布时间】:2021-01-18 04:29:23
【问题描述】:

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

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

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

谢谢。

【问题讨论】:

    标签: python-3.x opencv-python


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,这正是我要找的。我需要它来与 selenium 一起使用。
    猜你喜欢
    • 2014-09-11
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    • 2014-02-06
    • 2010-09-08
    相关资源
    最近更新 更多