| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- # -*- coding: utf-8 -*-
- # Author : Charley
- # Python : 3.10.8
- # Date : 2025/8/12 16:01
- """
- https://www.whatnot.com/live/fed63b2d-6a9d-4be9-a488-5e7a5440814c
- """
- import cv2
- import numpy as np
- import pyautogui
- import time
- from selenium import webdriver
- from selenium.webdriver.common.by import By
- from selenium.webdriver.chrome.options import Options
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.support import expected_conditions as EC
- from selenium.common.exceptions import TimeoutException
- class ScreenRecorder:
- def __init__(self, output_file='selenium_test.mp4', fps=60.0):
- self.screen_size = pyautogui.size()
- self.fps = fps
- self.fourcc = cv2.VideoWriter_fourcc(*'XVID')
- self.output_file = output_file
- self.out = None
- def start_recording(self):
- self.out = cv2.VideoWriter(self.output_file, self.fourcc, self.fps,
- (self.screen_size.width, self.screen_size.height))
- print("开始录制...")
- def record_for_duration(self, duration):
- start_time = time.time()
- while time.time() - start_time < duration:
- img = pyautogui.screenshot()
- frame = np.array(img)
- frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
- self.out.write(frame)
- def stop_recording(self):
- if self.out:
- self.out.release()
- cv2.destroyAllWindows()
- print("录制完成!")
- def main():
- # 配置参数
- url = 'https://www.whatnot.com/live/fed63b2d-6a9d-4be9-a488-5e7a5440814c'
- recording_duration = 10
- # 配置Chrome选项
- chrome_options = Options()
- chrome_options.add_argument("--headless=False") # 显示浏览器窗口
- chrome_options.add_argument("--start-maximized") # 最大化窗口
- # 初始化录制器
- recorder = ScreenRecorder()
- # 启动浏览器
- driver = webdriver.Chrome(options=chrome_options)
- try:
- # 开始录制
- recorder.start_recording()
- time.sleep(2)
- # 访问网页
- driver.get(url)
- wait = WebDriverWait(driver, 10)
- # 等待页面加载完成(可根据实际需要调整)
- time.sleep(2)
- # 这里可以添加对该网站的实际操作
- # 注意:原代码中的搜索框定位不适用于该网站
- # 录制指定时长
- recorder.record_for_duration(recording_duration)
- except TimeoutException:
- print("页面加载超时")
- except Exception as e:
- print(f"发生错误: {e}")
- finally:
- driver.quit()
- recorder.stop_recording()
- if __name__ == "__main__":
- main()
|