使用Golang如何对网站进行截图?

huangapple go评论81阅读模式
英文:

How to take screenshot of a website using Golang?

问题

我正在寻找一种方法,可以使用Golang给定一个URL并截取网站的屏幕截图。我搜索了一些结果,但没有找到任何有用的信息。请问有人可以帮助我吗?

英文:

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.

答案1

得分: 4

你可以使用Go版本的Selenium,如果你选择这个方案的话。https://godoc.org/github.com/tebeka/selenium

英文:

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

答案2

得分: 3

目前没有纯粹的 Golang 方法可以实现这一点,因为它必须涉及某种形式的浏览器。

实现此功能的最简单方法可能是:

  • 找一个好用的 NodeJS 库来截取网站截图
  • 创建一个适合你需求的 NodeJS 脚本来截取截图(输入/输出和设置)
  • 从 Golang 中执行这个 NodeJS 脚本,并在你的 Golang 代码中处理结果

这不是最干净的方法来完成这个任务 - 如果你想要更干净的方法,可能需要构建/找到一个控制浏览器的 Golang 包,这样你就可以跳过 NodeJS 中间人。

英文:

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.

答案3

得分: 2

我使用https://github.com/mafredri/cdp和一个Chrome无头docker容器解决了这个问题。

你可以在这里查看我的服务示例:https://gist.github.com/efimovalex/9f9b815b0d5b1b7889a51d46860faf8a

英文:

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

答案4

得分: 1

使用Go和Chrome/Chromium的一些其他工具包括:

英文:

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

答案5

得分: 0

我正在为这个特定任务编写一个程序。以下是一个浏览google.com并截取屏幕截图的示例代码。

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()
}

要安装该模块,请运行go get github.com/dreygur/webdriver

英文:

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

答案6

得分: 0

你可以使用chromedp。但是你需要安装Chrome浏览器!

示例:

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("截图已保存!")
}

英文:

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!")
}

huangapple
  • 本文由 发表于 2017年2月16日 20:28:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/42273997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定