Documentation
¶
Index ¶
- Variables
- func ClickDotXy(x, y float64) chromedp.Action
- func GetLastURL(lastUrl *string) chromedp.Action
- func IsSoft404NotFound(result **runtime.RemoteObject) chromedp.Action
- func KeyDownESC() chromedp.Action
- func MouseMoveTo(x, y float64) chromedp.Action
- func MoveMouse(x, y, x1, y1 float64) chromedp.Action
- func PathExists(path string) bool
- func RandFloat64(max int64) float64
- func RandFloat64MinMax(min, max int64) float64
- func RandInt64(max int64) int64
- func RandInt64MinMax(min, max int64) int64
- func RandInt64MinMaxDuration(min, max int64) time.Duration
- func RandMoveMouse() chromedp.Action
- func RandomKeyDown() chromedp.Action
- func ScrollToBottom() chromedp.Action
- func SleepRand(min, max int64) chromedp.Action
- func WaitDefineMetaReady(metaName string) chromedp.Action
- func WaitRootNodeVisible(rootNodeID string) chromedp.Action
- type Chrome
- func (c *Chrome) CleanCookie() *Chrome
- func (c *Chrome) CloseChrome()
- func (c *Chrome) Execute(target, node string, task chromedp.Tasks, timeout ...time.Duration) (string, *network.Response, error)
- func (c *Chrome) InitContext(timeout ...time.Duration)
- func (c *Chrome) OpenChrome()
- func (c *Chrome) SetCaptureScreenshot(savePath string) *Chrome
- func (c *Chrome) SetCookie(key, value, domain, path string, expired time.Duration, httpOnly, secure bool) *Chrome
- func (c *Chrome) SetDebugLogger(f func(string, ...interface{})) *Chrome
- func (c *Chrome) SetEmulate(emulate *device.Info) *Chrome
- func (c *Chrome) SetErrorLogger(f func(string, ...interface{})) *Chrome
- func (c *Chrome) SetFromUA(userAgentFrom string) *Chrome
- func (c *Chrome) SetInfoLogger(f func(string, ...interface{})) *Chrome
- func (c *Chrome) SetProxy(proxyUrl string) *Chrome
- func (c *Chrome) SetShowBrowser(show bool) *Chrome
Constants ¶
This section is empty.
Variables ¶
var ( // PcEmulate Pc版设备仿真参数 PcEmulate = device.Info{ Name: "chrome", UserAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36", Width: 1512, Height: 982, Scale: 1.000000, Landscape: false, Mobile: false, Touch: false, } // H5Emulate H5版设备仿真参数,也可以使用 device.IPhone13 之类的常量,但是无法自定义UA头 H5Emulate = device.Info{ Name: "iPhone 13", UserAgent: "Mozilla/5.0 (iPhone; CPU iPhone OS 17_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Mobile/15E148 Safari/604.1", Width: 390, Height: 844, Scale: 3.000000, Landscape: false, Mobile: true, Touch: true, } )
Functions ¶
func IsSoft404NotFound ¶
func IsSoft404NotFound(result **runtime.RemoteObject) chromedp.Action
IsSoft404NotFound 读取html的meta头判断当前页面是否为一个软404页面
前端需遵循软404页面通过js添加<meta name="robots" content="noindex"> - result 执行JS获取meta后返回的结果对象引用 判断是否有有添加软404的meta标记 result != nil && string(is404.Value) == "true"
func MouseMoveTo ¶
MouseMoveTo 鼠标移动到 (x,y) 坐标,内部添加上下100px的抖动
func RandFloat64MinMax ¶
RandFloat64MinMax 生成 [min,max) 范围内的float64类型的随机数
func RandInt64MinMaxDuration ¶
RandInt64MinMaxDuration 区间 [min,max)时长随机秒
func WaitDefineMetaReady ¶
WaitDefineMetaReady 等待自定义meta标签出现
- metaName meta标签name
对于SPA页面,可以约定SPA渲染完毕后往head标签里添加一个meta标签 例如:<meta name="metaName" content="true"> 然后crawler监听是否出现这个标签以侦测渲染完毕 每50毫秒侦测1次直到10秒后超时
Types ¶
type Chrome ¶
type Chrome struct { // context & cancelFunc Ctx context.Context // 执行任务的context // contains filtered or unexported fields }
Chrome chromedp浏览器控制结构
func (*Chrome) Execute ¶
func (c *Chrome) Execute(target, node string, task chromedp.Tasks, timeout ...time.Duration) (string, *network.Response, error)
Execute 执行任务方法抽象
- target 打开的网页URL
- node 打开网页指标元素ID,以等待网页已经渲染完毕
- task 补充的操作
- timeout 可选的超时时长,不传或传0表示发出一个请求不超时
返回值说明
- string 如果有html内容,则html字符串
- *network.Response chromedp原生resp
- error 是否报错
func (*Chrome) InitContext ¶
InitContext 手动初始化控制器context,方便需要Execute之前获取ctx场景
func (*Chrome) SetCaptureScreenshot ¶
SetCaptureScreenshot 设置截图存储文件夹路径
- savePath 存储截图的文件夹路径,路径需是存在可读写的, 例如 ./runtime/
使用示例:
c = crawler.New()
c.CaptureScreenshot("./runtime/").Crawler("https://www.baidu.com/", "", tasks)
func (*Chrome) SetCookie ¶
func (c *Chrome) SetCookie(key, value, domain, path string, expired time.Duration, httpOnly, secure bool) *Chrome
SetCookie 设置随请求发送出去的cookie
- key cookie键名
- value cookie键值
- domain cookie的有效域名,例如:.a.com 或 www.a.com
- path path路径,例如:/
- expired cookie有效时长
- httpOnly 是否httpOnly
- secure 是否secure安全
func (*Chrome) SetDebugLogger ¶
SetDebugLogger 设置调试logger
func (*Chrome) SetEmulate ¶
SetEmulate 设置爬虫抓取仿真参数
- emulate nil恢复默认行为即依据请求UA头自动选择使用 PcEmulate 或 H5Emulate,非nil则自定义
func (*Chrome) SetErrorLogger ¶
SetErrorLogger 设置错误logger
func (*Chrome) SetFromUA ¶
SetFromUA 设置请求来源UA头,如果要设置打开的chrome的自定义UA头请使用 SetEmulate 方法
- userAgent 设置请求来源UA头
本方法用于设置请求来源UA头设置
func (*Chrome) SetInfoLogger ¶
SetInfoLogger 设置记录logger
func (*Chrome) SetProxy ¶
SetProxy 设置代理服务器
-- proxyUrl 代理服务器url
参数错误不会报告异常,只有代理地址符合url规则才会被设置
c = chrome.New().SetProxy("xxx")