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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值