两个选项卡关闭第一个
package main
import (
"context"
"time"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
)
func main() {
// create the first tab
c, _ := chromedp.NewExecAllocator(context.Background(), chromedp.Flag("headless", false))
ctx1, _ := chromedp.NewContext(c)
chromedp.Run(ctx1)
// make second tab
ctx2, cancel := chromedp.NewContext(ctx1)
defer cancel() // close the second tab
chromedp.Run(ctx2)
// close the first tab, not closing the browser
if 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 tab
ctx1, cancel := chromedp.NewContext(context.Background())
defer cancel()
// ensure the first tab is created
if err := chromedp.Run(ctx1); err != nil {
panic(err)
}
// same browser, second tab
ctx2, _ := chromedp.NewContext(ctx1)
// ensure the second tab is created
if 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.Node
if err := chromedp.Nodes(xpath, &nodes, chromedp.AtLeast(0).Do(ctx); err != nil { return err }
if len(nodes) == 0 { return nil } // nothing to do
return chromedp.MouseClickNode(nodes[0]).Do(ctx)
}),
chromedp.Evaluate("window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });", &res),
)
多个urls访问抓取
package main
import (
"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 browser
err := 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,不显示GUI
chromedp.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一个请求链接