121 lines
3.1 KiB
Go
121 lines
3.1 KiB
Go
|
// 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() {
|
|||
|
//链接本地的浏览器 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 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.AddCookie(&selenium.Cookie{
|
|||
|
Name: "",
|
|||
|
Value: "",
|
|||
|
Path: "",
|
|||
|
Domain: "",
|
|||
|
Secure: false,
|
|||
|
Expiry: 0,
|
|||
|
})
|
|||
|
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()
|
|||
|
}
|