package util import ( "context" "fmt" "log" "os" "time" "github.com/chromedp/cdproto/emulation" "github.com/chromedp/cdproto/network" "github.com/chromedp/cdproto/page" "github.com/chromedp/chromedp" ) // ScreenshotURL 截图函数:传入网址、输出路径、宽高、四个边距和等待的元素 ID func ScreenshotURL(url, output string, width, height int, marginTop, marginRight, marginBottom, marginLeft int, waitClass string) error { // 创建一个上下文,连接到 Docker 中运行的 headless-shell 实例 remoteAllocatorCtx, cancel := chromedp.NewRemoteAllocator( context.Background(), "ws://127.0.0.1:9222/json/ws", ) defer cancel() ctx, cancel := chromedp.NewContext(remoteAllocatorCtx) defer cancel() // 设置超时时间,避免长时间无响应 ctx, cancel = context.WithTimeout(ctx, 60*time.Second) defer cancel() // 设置页面的宽高和缩放 if err := chromedp.Run(ctx, setViewportAndUserAgent(width, height, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:131.0) Gecko/20100101 Firefox/131.0")); err != nil { return fmt.Errorf("设置页面大小失败: %w", err) } // 启用网络请求拦截 if err := chromedp.Run(ctx, enableRequestInterception()); err != nil { return fmt.Errorf("启用请求拦截失败: %w", err) } // 用于存储截图的变量 var screenshot []byte queryAction := chromedp.WaitVisible(fmt.Sprintf(".%s", waitClass), chromedp.ByQuery) if waitClass == "" { queryAction = chromedp.WaitVisible(`body`, chromedp.ByQuery) } // 执行任务:打开网页并截图 err := chromedp.Run(ctx, chromedp.Navigate(url), // 打开网页 ignoreErrors(queryAction), // 等待指定元素 chromedp.ActionFunc(func(ctx context.Context) error { // 自定义截图逻辑 // 计算调整后的截图区域 clip := &page.Viewport{ X: float64(marginLeft), Y: float64(marginTop), Width: float64(width - marginLeft - marginRight), Height: float64(height - marginTop - marginBottom), Scale: 1.0, } var err error screenshot, err = page.CaptureScreenshot().WithClip(clip).Do(ctx) return err }), ) if err != nil { return fmt.Errorf("截图失败: %w", err) } // 保存截图到本地 if err := os.WriteFile(output, screenshot, 0644); err != nil { return fmt.Errorf("保存图片失败: %w", err) } return nil } func setViewportAndUserAgent(width, height int, userAgent string) chromedp.Tasks { return chromedp.Tasks{ emulation.SetDeviceMetricsOverride(int64(width), int64(height), 1.0, false). WithScreenOrientation(&emulation.ScreenOrientation{ Type: emulation.OrientationTypePortraitPrimary, Angle: 0, }), emulation.SetUserAgentOverride(userAgent), } } // enableRequestInterception 启用网络请求拦截,忽略不必要的资源加载(如广告、图片等) func enableRequestInterception() chromedp.Tasks { return chromedp.Tasks{ chromedp.ActionFunc(func(ctx context.Context) error { return network.Enable().Do(ctx) }), network.SetBlockedURLS([]string{ "pagead2.googlesyndication.com", "optimizationguide-pa.googleapis.com", }), } } // ignoreErrors 包裹 chromedp 任务,忽略执行过程中出现的错误 func ignoreErrors(task chromedp.Action) chromedp.ActionFunc { return chromedp.ActionFunc(func(ctx context.Context) error { err := task.Do(ctx) if err != nil { log.Printf("忽略错误: %v", err) } return nil }) }