两个选项卡关闭第一个
package mainimport ("context""time""github.com/chromedp/cdproto/cdp""github.com/chromedp/cdproto/page""github.com/chromedp/chromedp")func main() {// create the first tabc, _ := chromedp.NewExecAllocator(context.Background(), chromedp.Flag("headless", false))ctx1, _ := chromedp.NewContext(c)chromedp.Run(ctx1)// make second tabctx2, cancel := chromedp.NewContext(ctx1)defer cancel() // close the second tabchromedp.Run(ctx2)// close the first tab, not closing the browserif err := page.Close().Do(cdp.WithExecutor(ctx1, chromedp.FromContext(ctx1).Target)); err != nil {panic(err)}time.Sleep(time.Second * 2)// closing the second tab kills the browser}
多个选项卡
func ExampleNewContext_manyTabs() {// new browser, first tabctx1, cancel := chromedp.NewContext(context.Background())defer cancel()// ensure the first tab is createdif err := chromedp.Run(ctx1); err != nil {panic(err)}// same browser, second tabctx2, _ := chromedp.NewContext(ctx1)// ensure the second tab is createdif err := chromedp.Run(ctx2); err != nil {panic(err)}c1 := chromedp.FromContext(ctx1)c2 := chromedp.FromContext(ctx2)fmt.Printf("Same browser: %t\n", c1.Browser == c2.Browser)fmt.Printf("Same tab: %t\n", c1.Target == c2.Target)// Output:// Same browser: true// Same tab: false}
仅当存在节点时单击
err := chromedp.Run(ctx,chromedp.Navigate("[any_url_here]"),chromedp.ActionFunc(func(ctx context.Context) error {var nodes []*cdp.Nodeif err := chromedp.Nodes(xpath, &nodes, chromedp.AtLeast(0).Do(ctx); err != nil { return err }if len(nodes) == 0 { return nil } // nothing to doreturn chromedp.MouseClickNode(nodes[0]).Do(ctx)}),chromedp.Evaluate("window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });", &res),)
多个urls访问抓取
package mainimport ("context""log""time""github.com/chromedp/chromedp")func main() {options := append(chromedp.DefaultExecAllocatorOptions[:],chromedp.DisableGPU,chromedp.NoSandbox,chromedp.Flag("ignore-certificate-errors", true),chromedp.WindowSize(1400, 900),)aCtx, aCancel := chromedp.NewExecAllocator(context.Background(), options...)defer aCancel()ctx, cancel := chromedp.NewContext(aCtx)defer cancel()// a no-op action to allocate the browsererr := chromedp.Run(ctx, chromedp.ActionFunc(func(_ context.Context) error {return nil}))if err != nil {log.Fatalln(err)}urls := []string{"https://www.google.com", "https://www.bing.com"}for _, url := range urls {func() {xCtx, xCancel := context.WithTimeout(ctx, time.Second*90)defer xCancel()err := chromedp.Run(xCtx, chromedp.Tasks{chromedp.Navigate(url),})if err != nil {log.Printf("%s failed: %s ", url, err)return}log.Println("success: ", url)}()}}
chromedp一些options
opts := append(chromedp.DefaultExecAllocatorOptions[:],// 禁用GPU,不显示GUIchromedp.DisableGPU,// 取消沙盒模式chromedp.NoSandbox,// 指定浏览器分辨率//chromedp.WindowSize(1600, 900),// 设置UA,防止有些页面识别headless模式chromedp.UserAgent(utils.UserAgent),// 隐身模式启动chromedp.Flag("incognito", true),// 忽略证书错误chromedp.Flag("ignore-certificate-errors", true),// 窗口最大化chromedp.Flag("start-maximized", true),// 不加载图片, 提升速度chromedp.Flag("disable-images", true),chromedp.Flag("blink-settings", "imagesEnabled=false"),// 禁用扩展chromedp.Flag("disable-extensions", true),// 禁止加载所有插件chromedp.Flag("disable-plugins", true),// 禁用浏览器应用chromedp.Flag("disable-software-rasterizer", true),//chromedp.Flag("remote-debugging-port","9222"),//chromedp.Flag("debuggerAddress","127.0.0.1:9222"),chromedp.Flag("user-data-dir", "./.cache"),//chromedp.Flag("excludeSwitches", "enable-automation"),// 设置用户数据目录//chromedp.UserDataDir(dir),//chromedp.ExecPath("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"),)
并行两个协程就出验证码,且继续并行两个协程验证码无效
chromedp提交验证码速度过快也会导致验证码无效
there an easy way to set cookie?
Test
case1:
- one channel
- 平均每2s一个请求链接
