util/chrome.go
2023-02-11 20:09:03 +08:00

124 lines
3.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package util ...
//
// Description : util ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-01-24 15:36
package util
import (
"fmt"
"io/ioutil"
"github.com/tebeka/selenium"
"github.com/tebeka/selenium/chrome"
)
// 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://detail.tmall.hk/hk/item.htm?id=605481322232&rn=bbb58fe2f3e13e03b21df9c396f23fbc&abbucket=12"
w_b1, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", c.port))
if err != nil {
fmt.Println("connect to the webDriver faild", err.Error())
return
}
_ = w_b1.Get(url)
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()
_ = ioutil.WriteFile("test.png", imgByteData, 0777)
//获取网页源码
pageSource, err := w_b1.PageSource()
if err != nil {
fmt.Println("get page faild", 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()
}