可以的可以使用chromedp库。
package main
import (
"context"
"io/ioutil"
"log"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
)
func main() {
ctx, cancel := chromedp.NewContext(context.Background(), chromedp.WithDebugf(log.Printf))
defer cancel()
url := "https://golang-tech-stack.com/"
filename := "golang.png"
var imageBuf []byte
if err := chromedp.Run(ctx, ScreenshotTasks(url, &imageBuf)); err != nil {
log.Fatal(err)
}
if err := ioutil.WriteFile(filename, imageBuf, 0644); err != nil {
log.Fatal(err)
}
}
func ScreenshotTasks(url string, imageBuf *[]byte) chromedp.Tasks {
return chromedp.Tasks{
chromedp.Navigate(url),
chromedp.ActionFunc(func(ctx context.Context) (err error) {
*imageBuf, err = page.CaptureScreenshot().WithQuality(100).Do(ctx)
return err
}),
}
}