go-rod库的使用

简单测试了一下,很神奇
先跑一个可以被 Rod 使用的 Headless 浏览器容器环境
以 macOS 为例:

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222 --headless

再跑代码就可以了

package main

import (
	"fmt"
	"github.com/go-rod/rod"
	"github.com/go-rod/rod/lib/launcher"
)

func main() {
    
    
	//page := rod.New().MustConnect().MustPage("https://www.wikipedia.org/")
	//page := rod.New().MustConnect().MustPage("https://github.com/")
	//page.MustWaitLoad().MustScreenshot("a.png")

	//container := launcher.MustResolveURL("127.0.0.1:9222")
	//browser := rod.New().ControlURL(container).MustConnect().MustPage("https://www.bilibili.com/read/home")
	//
	//fmt.Println(
	//	browser.MustEval("() => document.title"),
	//)

	container := launcher.MustResolveURL("127.0.0.1:9222")
	browser := rod.New().ControlURL(container).MustConnect().MustPage("https://www.bilibili.com/read/home")

	client := browser.
		MustWaitLoad().
		MustElement(".article-item")

	result := client.MustEval(`() => Array.from(document.querySelectorAll('.article-list .article-item .article-title')).map(article=>article.innerText).join("\n")`)

	fmt.Println(result)
}

在这里插入图片描述
在这里插入图片描述

GOROOT=/Users/hanpeng/go/go1.17.13 #gosetup
GOPATH=/Users/hanpeng/go #gosetup
/Users/hanpeng/go/go1.17.13/bin/go build -o /private/var/folders/yh/csp_w3d93v191v8l_3d_xhp00000gn/T/GoLand/___go_build_testaws testaws #gosetup
/private/var/folders/yh/csp_w3d93v191v8l_3d_xhp00000gn/T/GoLand/___go_build_testaws
“从Jr.时起平野紫耀&永濑廉就是人气最高…”杰尼斯Jr.们提“当时年长Jr.间的明争暗斗
《玉子爱情故事》超详细万字逐帧解析(下)——传达爱意的纸杯电话
“今年办不起年会可以直说,别用这脏东西羞辱我”
百废待兴,阴阳怪气——2023年春节档电影观后感
《满江红》:绝代佳人和国士无双
当天灾军团席卷这个世界的时候,我们应该怎么办?
你给《中国奇谭》的五星,有多少情怀分?
《玉子爱情故事》超详细万字逐帧解析(中)——饼藏青涩的告白
县城学生不能失去衡水二中
第一人称写作,需要注意哪些细节?丨新春盛宴·第一人称主题破题
收入占比不到1%,不配拥有姓名的国行主机都在干什么?
《卧龙 苍天陨落》制作人采访:想描绘黑暗幻想风的三国故事
搞艺术,不能总那么脏,对吧?——评艺画《三体》

Process finished with the exit code 0

猜你喜欢

转载自blog.csdn.net/qq_37106501/article/details/129056544