两个选项卡关闭第一个

  1. package main
  2. import (
  3. "context"
  4. "time"
  5. "github.com/chromedp/cdproto/cdp"
  6. "github.com/chromedp/cdproto/page"
  7. "github.com/chromedp/chromedp"
  8. )
  9. func main() {
  10. // create the first tab
  11. c, _ := chromedp.NewExecAllocator(context.Background(), chromedp.Flag("headless", false))
  12. ctx1, _ := chromedp.NewContext(c)
  13. chromedp.Run(ctx1)
  14. // make second tab
  15. ctx2, cancel := chromedp.NewContext(ctx1)
  16. defer cancel() // close the second tab
  17. chromedp.Run(ctx2)
  18. // close the first tab, not closing the browser
  19. if err := page.Close().Do(cdp.WithExecutor(ctx1, chromedp.FromContext(ctx1).Target)); err != nil {
  20. panic(err)
  21. }
  22. time.Sleep(time.Second * 2)
  23. // closing the second tab kills the browser
  24. }

多个选项卡

  1. func ExampleNewContext_manyTabs() {
  2. // new browser, first tab
  3. ctx1, cancel := chromedp.NewContext(context.Background())
  4. defer cancel()
  5. // ensure the first tab is created
  6. if err := chromedp.Run(ctx1); err != nil {
  7. panic(err)
  8. }
  9. // same browser, second tab
  10. ctx2, _ := chromedp.NewContext(ctx1)
  11. // ensure the second tab is created
  12. if err := chromedp.Run(ctx2); err != nil {
  13. panic(err)
  14. }
  15. c1 := chromedp.FromContext(ctx1)
  16. c2 := chromedp.FromContext(ctx2)
  17. fmt.Printf("Same browser: %t\n", c1.Browser == c2.Browser)
  18. fmt.Printf("Same tab: %t\n", c1.Target == c2.Target)
  19. // Output:
  20. // Same browser: true
  21. // Same tab: false
  22. }

仅当存在节点时单击

  1. err := chromedp.Run(ctx,
  2. chromedp.Navigate("[any_url_here]"),
  3. chromedp.ActionFunc(func(ctx context.Context) error {
  4. var nodes []*cdp.Node
  5. if err := chromedp.Nodes(xpath, &nodes, chromedp.AtLeast(0).Do(ctx); err != nil { return err }
  6. if len(nodes) == 0 { return nil } // nothing to do
  7. return chromedp.MouseClickNode(nodes[0]).Do(ctx)
  8. }),
  9. chromedp.Evaluate("window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });", &res),
  10. )

多个urls访问抓取

  1. package main
  2. import (
  3. "context"
  4. "log"
  5. "time"
  6. "github.com/chromedp/chromedp"
  7. )
  8. func main() {
  9. options := append(chromedp.DefaultExecAllocatorOptions[:],
  10. chromedp.DisableGPU,
  11. chromedp.NoSandbox,
  12. chromedp.Flag("ignore-certificate-errors", true),
  13. chromedp.WindowSize(1400, 900),
  14. )
  15. aCtx, aCancel := chromedp.NewExecAllocator(context.Background(), options...)
  16. defer aCancel()
  17. ctx, cancel := chromedp.NewContext(aCtx)
  18. defer cancel()
  19. // a no-op action to allocate the browser
  20. err := chromedp.Run(ctx, chromedp.ActionFunc(func(_ context.Context) error {
  21. return nil
  22. }))
  23. if err != nil {
  24. log.Fatalln(err)
  25. }
  26. urls := []string{"https://www.google.com", "https://www.bing.com"}
  27. for _, url := range urls {
  28. func() {
  29. xCtx, xCancel := context.WithTimeout(ctx, time.Second*90)
  30. defer xCancel()
  31. err := chromedp.Run(xCtx, chromedp.Tasks{
  32. chromedp.Navigate(url),
  33. })
  34. if err != nil {
  35. log.Printf("%s failed: %s ", url, err)
  36. return
  37. }
  38. log.Println("success: ", url)
  39. }()
  40. }
  41. }

chromedp一些options

  1. opts := append(chromedp.DefaultExecAllocatorOptions[:],
  2. // 禁用GPU,不显示GUI
  3. chromedp.DisableGPU,
  4. // 取消沙盒模式
  5. chromedp.NoSandbox,
  6. // 指定浏览器分辨率
  7. //chromedp.WindowSize(1600, 900),
  8. // 设置UA,防止有些页面识别headless模式
  9. chromedp.UserAgent(utils.UserAgent),
  10. // 隐身模式启动
  11. chromedp.Flag("incognito", true),
  12. // 忽略证书错误
  13. chromedp.Flag("ignore-certificate-errors", true),
  14. // 窗口最大化
  15. chromedp.Flag("start-maximized", true),
  16. // 不加载图片, 提升速度
  17. chromedp.Flag("disable-images", true),
  18. chromedp.Flag("blink-settings", "imagesEnabled=false"),
  19. // 禁用扩展
  20. chromedp.Flag("disable-extensions", true),
  21. // 禁止加载所有插件
  22. chromedp.Flag("disable-plugins", true),
  23. // 禁用浏览器应用
  24. chromedp.Flag("disable-software-rasterizer", true),
  25. //chromedp.Flag("remote-debugging-port","9222"),
  26. //chromedp.Flag("debuggerAddress","127.0.0.1:9222"),
  27. chromedp.Flag("user-data-dir", "./.cache"),
  28. //chromedp.Flag("excludeSwitches", "enable-automation"),
  29. // 设置用户数据目录
  30. //chromedp.UserDataDir(dir),
  31. //chromedp.ExecPath("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"),
  32. )

并行两个协程就出验证码,且继续并行两个协程验证码无效

chromedp提交验证码速度过快也会导致验证码无效

there an easy way to set cookie?

link

Test

case1:

  • one channel
  • 平均每2s一个请求链接