golang chromeDp 使用
借助谷歌开发者工具协议(CDP,Chrome DevTools Protocol)远程与浏览器交互
在Go中实现了Chrome DevTools协议的知名第三方库是 chromedp
linux 界面搭建 chromeDp 环境
docker-compose 搭建 headless-shell
version: '3.1'
services:
chrome:
image: chromedp/headless-shell:latest
ports:
- "9222:9222"
获取信息
curl localhost:9222/json/version
{
"Browser": "Chrome/114.0.5735.199",
"Protocol-Version": "1.3",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.199 Safari/537.36",
"V8-Version": "11.4.183.25",
"WebKit-Version": "537.36 (@581ada08cf738a4eb44f712c6f8cd40030e5c1a0)",
"webSocketDebuggerUrl": "ws://localhost:9222/devtools/browser/5b3453d9-d437-4d7d-8f23-633d10b96a15"
}
golang 使用
func main() {
// arg : ws://url:9222/
c, cancel := chromedp.NewRemoteAllocator(context.Background(), os.Args[1])
defer cancel()
ctx, cancel2 := chromedp.NewContext(c)
defer cancel2()
... // logic
}