如何使用golang拍摄网站的屏幕截图?

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.

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.

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

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