LoginSignup
1
3

More than 5 years have passed since last update.

go言語でHeadlessChromeが使いたい!

Posted at

前準備

必要なパッケージは自分でinstallしてね

セットアップコード

//多分使うのはこれだけだった(はず)
import (
    "github.com/sclevine/agouti"
)
// Chromeを利用することを宣言
agoutiDriver := agouti.ChromeDriver()
agoutiDriver.Start()
defer agoutiDriver.Stop()
var ua = "ユーザーエージェント"
page, _:= agoutiDriver.NewPage(agouti.Desired(agouti.Capabilities{
    "chromeOptions": map[string][]string{
        "args": []string{
            "--headless",
            "--disable-gpu",
            "--user-agent="+ua+"",
            "window-size=1280x800",
            "--log-level=3",
            "--no-sandbox",
        },
    },
}),
)

これで、ベースとなる設定は完了です。

実行(起動)

//ページ移動
page.Navigate("URL")
//JSの実行
page.RunScript("動かしたいJS",nil, nil)

基本的にページの移動+JSの実行ができれば満足では??
以上!

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3