crawler

package module
v0.0.0-...-953e135 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 20, 2025 License: GPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 ClickDotXy

func ClickDotXy(x, y float64) chromedp.Action

ClickDotXy 点击

func GetLastURL

func GetLastURL(lastUrl *string) chromedp.Action

GetLastURL 页面可能会出现Redirect,用于获取最后停留页面URL

  • lastUrl 最后停留页面的URL

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 KeyDownESC

func KeyDownESC() chromedp.Action

KeyDownESC 按下键盘ESC键

func MouseMoveTo

func MouseMoveTo(x, y float64) chromedp.Action

MouseMoveTo 鼠标移动到 (x,y) 坐标,内部添加上下100px的抖动

func MoveMouse

func MoveMouse(x, y, x1, y1 float64) chromedp.Action

func PathExists

func PathExists(path string) bool

PathExists 判断文件|文件夹是否存在

func RandFloat64

func RandFloat64(max int64) float64

RandFloat64 生成 [0,max) 范围内的float64类型的随机数

func RandFloat64MinMax

func RandFloat64MinMax(min, max int64) float64

RandFloat64MinMax 生成 [min,max) 范围内的float64类型的随机数

func RandInt64

func RandInt64(max int64) int64

RandInt64 生成 [0,max) 范围内的int64类型随机数

func RandInt64MinMax

func RandInt64MinMax(min, max int64) int64

RandInt64MinMax 区间 [min,max)

func RandInt64MinMaxDuration

func RandInt64MinMaxDuration(min, max int64) time.Duration

RandInt64MinMaxDuration 区间 [min,max)时长随机秒

func RandMoveMouse

func RandMoveMouse() chromedp.Action

RandMoveMouse 按多条预定义轨迹坐标移动

func RandomKeyDown

func RandomKeyDown() chromedp.Action

RandomKeyDown 随机按下键盘上一个按键

func ScrollToBottom

func ScrollToBottom() chromedp.Action

ScrollToBottom 下拉滚动条到最底部

通过JS下拉网页滚动条到最底部以触发一些滚动加载的逻辑

func SleepRand

func SleepRand(min, max int64) chromedp.Action

SleepRand 随机休眠 [min, max) 范围内秒

func WaitDefineMetaReady

func WaitDefineMetaReady(metaName string) chromedp.Action

WaitDefineMetaReady 等待自定义meta标签出现

  • metaName meta标签name

对于SPA页面,可以约定SPA渲染完毕后往head标签里添加一个meta标签 例如:<meta name="metaName" content="true"> 然后crawler监听是否出现这个标签以侦测渲染完毕 每50毫秒侦测1次直到10秒后超时

func WaitRootNodeVisible

func WaitRootNodeVisible(rootNodeID string) chromedp.Action

WaitRootNodeVisible 等待根节点可见

  • rootNodeID html根节点ID

Types

type Chrome

type Chrome struct {
	// context & cancelFunc
	Ctx context.Context // 执行任务的context
	// contains filtered or unexported fields
}

Chrome chromedp浏览器控制结构

func New

func New() *Chrome

New 初始化

func (*Chrome) CleanCookie

func (c *Chrome) CleanCookie() *Chrome

CleanCookie 清理启动时设置的cookie

func (*Chrome) CloseChrome

func (c *Chrome) CloseChrome()

CloseChrome context形式关闭chrome

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

func (c *Chrome) InitContext(timeout ...time.Duration)

InitContext 手动初始化控制器context,方便需要Execute之前获取ctx场景

func (*Chrome) OpenChrome

func (c *Chrome) OpenChrome()

OpenChrome 打开chrome并初始化各参数

func (*Chrome) SetCaptureScreenshot

func (c *Chrome) SetCaptureScreenshot(savePath string) *Chrome

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

func (c *Chrome) SetDebugLogger(f func(string, ...interface{})) *Chrome

SetDebugLogger 设置调试logger

func (*Chrome) SetEmulate

func (c *Chrome) SetEmulate(emulate *device.Info) *Chrome

SetEmulate 设置爬虫抓取仿真参数

  • emulate nil恢复默认行为即依据请求UA头自动选择使用 PcEmulate 或 H5Emulate,非nil则自定义

func (*Chrome) SetErrorLogger

func (c *Chrome) SetErrorLogger(f func(string, ...interface{})) *Chrome

SetErrorLogger 设置错误logger

func (*Chrome) SetFromUA

func (c *Chrome) SetFromUA(userAgentFrom string) *Chrome

SetFromUA 设置请求来源UA头,如果要设置打开的chrome的自定义UA头请使用 SetEmulate 方法

  • userAgent 设置请求来源UA头

本方法用于设置请求来源UA头设置

func (*Chrome) SetInfoLogger

func (c *Chrome) SetInfoLogger(f func(string, ...interface{})) *Chrome

SetInfoLogger 设置记录logger

func (*Chrome) SetProxy

func (c *Chrome) SetProxy(proxyUrl string) *Chrome

SetProxy 设置代理服务器

-- proxyUrl 代理服务器url

参数错误不会报告异常,只有代理地址符合url规则才会被设置

c = chrome.New().SetProxy("xxx")

func (*Chrome) SetShowBrowser

func (c *Chrome) SetShowBrowser(show bool) *Chrome

SetShowBrowser 设置是否显示浏览器界面UI

-- show 是否显示浏览器UI

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL