From be17fdeb51de9482ab7a6f4cd6ff6f82011b4750 Mon Sep 17 00:00:00 2001 From: ztino <> Date: Mon, 4 Jan 2021 23:57:30 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E8=8E=B7=E5=8F=96eid?= =?UTF-8?q?=E5=92=8Cfp=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/jdTdudfp.go | 51 +++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/cmd/jdTdudfp.go b/cmd/jdTdudfp.go index eb1c676..329efd3 100644 --- a/cmd/jdTdudfp.go +++ b/cmd/jdTdudfp.go @@ -11,8 +11,6 @@ import ( "github.com/ztino/jd_seckill/common" "github.com/ztino/jd_seckill/jd_seckill" "log" - "net/http" - "net/http/httptest" "net/url" "time" ) @@ -34,16 +32,20 @@ func startJdTdudfp(cmd *cobra.Command, args []string) { log.Println("自动获取eid和fp失败,请重新登录") }else { log.Println("开始自动获取eid和fp,如遇卡住请结束进程,重新启动") - ctx := context.Background() options := []chromedp.ExecAllocatorOption{ chromedp.Flag("headless", false), - chromedp.Flag("hide-scrollbars", false), - chromedp.Flag("mute-audio", false), + //chromedp.Flag("blink-settings", "imagesEnabled=false"), + chromedp.Flag("start-maximized", true), + chromedp.Flag("no-sandbox",true), + chromedp.Flag("disable-setuid-sandbox",true), + chromedp.Flag("no-default-browser-check",true), + chromedp.Flag("disable-plugins",true), + chromedp.WindowSize(1920,1080), chromedp.UserAgent(common.Config.MustValue("config","default_user_agent","")), } options = append(chromedp.DefaultExecAllocatorOptions[:], options...) - c, cc := chromedp.NewExecAllocator(ctx, options...) + c, cc := chromedp.NewExecAllocator(context.Background(), options...) defer cc() ctx, cancel := chromedp.NewContext(c) @@ -52,7 +54,7 @@ func startJdTdudfp(cmd *cobra.Command, args []string) { u, _ := url.Parse("http://jd.com") cookies := common.CookieJar.Cookies(u) - err := chromedp.Run(ctx, + err = chromedp.Run(ctx, chromedp.Tasks{ chromedp.ActionFunc(func(ctx context.Context) error { expr := cdp.TimeSinceEpoch(time.Now().Add(180 * 24 * time.Hour)) @@ -66,42 +68,46 @@ func startJdTdudfp(cmd *cobra.Command, args []string) { return nil }), chromedp.Navigate("https://jd.com"), + chromedp.Sleep(2*time.Second), chromedp.Click(".cate_menu_lk"), + chromedp.Sleep(2*time.Second), }, ) if err != nil { log.Fatal(err) } - newCtx, cancel := chromedp.NewContext(ctx, chromedp.WithTargetID(<-ch)) - ch = addNewTabListener(newCtx) - defer cancel() + + newCtx, cancel2 := chromedp.NewContext(ctx, chromedp.WithTargetID(<-ch)) + ch2 := addNewTabListener(newCtx) + defer cancel2() err = chromedp.Run(newCtx, - chromedp.Click(`.goods_item_link`), + chromedp.Click(".goods_item_link"), + chromedp.Sleep(4*time.Second), ) if err != nil { log.Fatal(err) } - newCtx, cancel = chromedp.NewContext(ctx, chromedp.WithTargetID(<-ch)) - defer cancel() + newCtx2, cancel3 := chromedp.NewContext(ctx, chromedp.WithTargetID(<-ch2)) + defer cancel3() var res []byte - err = chromedp.Run(newCtx, - chromedp.Click(`#InitCartUrl`), - chromedp.WaitVisible(".btn-addtocart"), + err = chromedp.Run(newCtx2, + chromedp.Click("#InitCartUrl"), + chromedp.Sleep(2*time.Second), chromedp.Click(".btn-addtocart"), - chromedp.WaitVisible(".common-submit-btn"), + chromedp.Sleep(2*time.Second), chromedp.Click(".common-submit-btn"), - chromedp.WaitVisible("#sumPayPriceId"), chromedp.Sleep(3*time.Second), chromedp.Evaluate("_JdTdudfp", &res), ) if err != nil { log.Fatal(err) } + value:=string(res) - if !gjson.Valid(value) { + if !gjson.Valid(value) || gjson.Get(value,"eid").String()=="" || gjson.Get(value,"fp").String()=="" { log.Println("获取失败,请重新尝试,返回信息:"+value) }else{ log.Println("获取成功,请手动填入配置文件") @@ -112,11 +118,10 @@ func startJdTdudfp(cmd *cobra.Command, args []string) { } func addNewTabListener(ctx context.Context) <-chan target.ID { - mux := http.NewServeMux() +/* mux := http.NewServeMux() ts := httptest.NewServer(mux) - defer ts.Close() - + defer ts.Close()*/ return chromedp.WaitNewTarget(ctx, func(info *target.Info) bool { - return info.URL != "" + return true }) } \ No newline at end of file