2023-01-24 17:14:44 +08:00
// Package util ...
//
// Description : util ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-01-24 15:36
package util
import (
"fmt"
"github.com/tebeka/selenium"
"github.com/tebeka/selenium/chrome"
2023-07-27 18:21:51 +08:00
"os"
2023-01-24 17:14:44 +08:00
)
// NewChrome ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:38 2023/1/24
//
// chromedriver下载地址 : http://chromedriver.storage.googleapis.com/index.html
func NewChrome ( seleniumPath string , port int , debug bool , options [ ] selenium . ServiceOption ) ( * Chrome , error ) {
instance := & Chrome {
seleniumPath : seleniumPath ,
port : port ,
debug : debug ,
}
// 设置debug模式
selenium . SetDebug ( instance . debug )
var (
err error
)
// 获取实例
if instance . serviceInstance , err = selenium . NewChromeDriverService ( seleniumPath , port , options ... ) ; nil != err {
return nil , err
}
return instance , nil
}
// Chrome 无头模式chrome浏览器, 用于爬虫或截图
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:36 2023/1/24
type Chrome struct {
seleniumPath string // chrome 路径
port int // 端口
debug bool // 开启debug模式
serviceInstance * selenium . Service // 浏览器实例
}
// Render 无头模式渲染页面
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:14 2023/1/24
2023-01-24 18:50:43 +08:00
func ( c * Chrome ) Render ( cookieList [ ] * selenium . Cookie ) {
2023-01-24 17:14:44 +08:00
//链接本地的浏览器 chrome
caps := selenium . Capabilities {
//"browserName": "/Applications/Google Chrome Dev.app/Contents/MacOS/Google Chrome Dev",
"browserName" : "Google Chrome" ,
}
//禁止图片加载,加快渲染速度
imagCaps := map [ string ] interface { } {
"profile.managed_default_content_settings.images" : 2 ,
}
chromeCaps := chrome . Capabilities {
Prefs : imagCaps ,
Path : "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" ,
Args : [ ] string {
//静默执行请求
"--headless" , // 设置Chrome无头模式, 在linux下运行, 需要设置这个参数, 否则会报错
"--no-sandbox" ,
2023-02-11 20:09:03 +08:00
"--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36" , // 模拟user-agent, 防反爬
2023-01-24 17:14:44 +08:00
} ,
}
//以上是设置浏览器参数
caps . AddChrome ( chromeCaps )
2023-01-24 18:50:43 +08:00
2023-07-27 18:21:51 +08:00
url := "https://zhangdeman.cn/archives/bbd86236.html"
2023-01-24 17:14:44 +08:00
w_b1 , err := selenium . NewRemote ( caps , fmt . Sprintf ( "http://localhost:%d/wd/hub" , c . port ) )
if err != nil {
2023-07-27 18:21:51 +08:00
fmt . Println ( "connect to the webDriver failed" , err . Error ( ) )
2023-01-24 17:14:44 +08:00
return
}
2023-01-24 18:50:43 +08:00
_ = w_b1 . Get ( url )
2023-07-27 18:21:51 +08:00
fmt . Println ( w_b1 . DoubleClick ( ) )
2023-01-24 18:50:43 +08:00
for _ , itemCookie := range cookieList {
err = w_b1 . AddCookie ( itemCookie )
2023-02-11 20:09:03 +08:00
if nil != err {
fmt . Println ( err )
}
2023-01-24 18:50:43 +08:00
}
allCookie , _ := w_b1 . GetCookies ( )
fmt . Println ( allCookie )
2023-01-24 17:14:44 +08:00
err = w_b1 . Get ( url )
imgByteData , _ := w_b1 . Screenshot ( )
2023-07-27 18:21:51 +08:00
_ = os . WriteFile ( "test.png" , imgByteData , 0777 )
2023-01-24 17:14:44 +08:00
//获取网页源码
pageSource , err := w_b1 . PageSource ( )
if err != nil {
2023-07-27 18:21:51 +08:00
fmt . Println ( "get page failed" , err . Error ( ) )
2023-01-24 17:14:44 +08:00
return
}
fmt . Print ( pageSource )
}
// Close 关闭浏览器
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:52 2023/1/24
func ( c * Chrome ) Close ( ) error {
return c . serviceInstance . Stop ( )
}