How to take screenshot of a website using Golang?
Asked Answered
D

6

5

What I'm looking to do, given a URL and take a screenshot of the website using Golang. I searched for results but I didn't get any. Can anyone please help me.

Darlinedarling answered 16/2, 2017 at 12:28 Comment(2)
Would require a rendering engine in pure Go. There isn't any. Use PhantomJS or something like this.Weimer
Tell user to upload a screenshot by pressing "Print Screen" Button.Maximin
U
4

You can use a Go version of Selenium if you want to go that route. https://godoc.org/github.com/tebeka/selenium

Untinged answered 16/2, 2017 at 18:26 Comment(0)
S
3

There is no pure golang way to do at the moment this since it must involve a browser is some form.

The easiest path to achieve this functionality is probably:

  • Find a nice NodeJS library to take website screenshots
  • Create a NodeJS script that is suits your needs for taking screenshots (i/o and settings)
  • Execute this NodeJS script from Golang and handle the results in your Golang code

Not the cleanest method to get this done though - if you want it cleaner you probably have to build/find a golang package that controls a browser so you can skip the NodeJS middleman.

Studdingsail answered 16/2, 2017 at 12:46 Comment(0)
M
3

I solved this issue using https://github.com/mafredri/cdp and a Chrome headless docker container.

You can see my service example here: https://gist.github.com/efimovalex/9f9b815b0d5b1b7889a51d46860faf8a

Mediterranean answered 14/5, 2018 at 8:40 Comment(0)
M
2

A few more tools using Go and Chrome/Chromium include:

Mendelevium answered 4/12, 2019 at 13:46 Comment(0)
S
0

I was writing a program for this specific task. Here is a sample code that browse google.com and takes a screenshot.

package main

import (
    "time"

    driver "github.com/dreygur/webdriver"
)

func main() {
    url := `https://google.com`
    driver.RunServer("./geckodriver")
    driver.GetSession()
    driver.Get(url)
    time.Sleep(8 * time.Second)
    driver.Screenshot("google")
    time.Sleep(8 * time.Second)

    defer driver.Kill()
}

To install the module, run go get github.com/dreygur/webdriver

Snappy answered 11/2, 2021 at 23:0 Comment(0)
N
0

You can use chromedp. But you need install chrome browser!

Example :

package main

import (
    "context"
    "fmt"
    "os"
    "time"

    "github.com/chromedp/chromedp"
)

func TackScreenShot(ctx context.Context, url string) ([]byte, error) {
    context, cancel := chromedp.NewContext(ctx)
    defer cancel()

    var filebyte []byte
    if err := chromedp.Run(context, chromedp.Tasks{
        chromedp.Navigate(url),
        chromedp.Sleep(3 * time.Second),
        chromedp.CaptureScreenshot(&filebyte),
    }); err != nil {
        return nil, err
    }
    return filebyte, nil
}

func main() {
    url := "https://google.com"
    ctx := context.TODO()

    data, err := TackScreenShot(ctx, url)
    if err != nil {
        panic(err)
    }

    defer ctx.Done()

    pngFile, err := os.Create("./shot.png")
    if err != nil {
        panic(err)
    }

    defer pngFile.Close()

    pngFile.Write(data)
    fmt.Println("screen shot tacked!")
}

Neodymium answered 7/8, 2022 at 12:56 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.