124 lines
3.1 KiB
Go
124 lines
3.1 KiB
Go
// 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"
|
||
"os"
|
||
)
|
||
|
||
// 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
|
||
func (c *Chrome) Render(cookieList []*selenium.Cookie) {
|
||
//链接本地的浏览器 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",
|
||
"--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,防反爬
|
||
},
|
||
}
|
||
//以上是设置浏览器参数
|
||
caps.AddChrome(chromeCaps)
|
||
|
||
url := "https://zhangdeman.cn/archives/bbd86236.html"
|
||
w_b1, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", c.port))
|
||
if err != nil {
|
||
fmt.Println("connect to the webDriver failed", err.Error())
|
||
return
|
||
}
|
||
_ = w_b1.Get(url)
|
||
fmt.Println(w_b1.DoubleClick())
|
||
|
||
for _, itemCookie := range cookieList {
|
||
err = w_b1.AddCookie(itemCookie)
|
||
if nil != err {
|
||
fmt.Println(err)
|
||
}
|
||
}
|
||
allCookie, _ := w_b1.GetCookies()
|
||
fmt.Println(allCookie)
|
||
err = w_b1.Get(url)
|
||
imgByteData, _ := w_b1.Screenshot()
|
||
_ = os.WriteFile("test.png", imgByteData, 0777)
|
||
|
||
//获取网页源码
|
||
pageSource, err := w_b1.PageSource()
|
||
if err != nil {
|
||
fmt.Println("get page failed", err.Error())
|
||
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()
|
||
}
|