Skip to content

Commit

Permalink
修复获取eid和fp,浏览器卡住不动
Browse files Browse the repository at this point in the history
  • Loading branch information
ztino committed Jan 5, 2021
1 parent 3227cdd commit 4bf0645
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
/qr_code.png
/cookie.txt
*.bak
go.sum
67 changes: 23 additions & 44 deletions cmd/jdTdudfp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package cmd

import (
"context"
"fmt"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/cdproto/target"
"github.com/chromedp/chromedp"
"github.com/spf13/cobra"
"github.com/tidwall/gjson"
Expand All @@ -15,12 +13,13 @@ import (
"log"
"net/url"
"os"
"strconv"
"time"
)

func init() {
rootCmd.AddCommand(jdTdudfpCmd)
jdTdudfpCmd.Flags().StringP("good_url","g","","")
_=jdTdudfpCmd.MarkFlagRequired("good_url")
}

var jdTdudfpCmd = &cobra.Command{
Expand All @@ -35,8 +34,7 @@ func startJdTdudfp(cmd *cobra.Command, args []string) {
if err != nil {
log.Println("自动获取eid和fp失败,请重新登录")
} else {
retryTimes, _ := strconv.Atoi(common.Config.MustValue("config", "retry_times", "5"))

log.Println("开始自动获取eid和fp,如遇卡住请结束进程,重新启动")
options := []chromedp.ExecAllocatorOption{
chromedp.Flag("headless", false), //debug使用
chromedp.Flag("blink-settings", "imagesEnabled=false"), //禁用图片加载
Expand All @@ -53,18 +51,23 @@ func startJdTdudfp(cmd *cobra.Command, args []string) {
defer cc()

ctx, cancel := chromedp.NewContext(c)
_ = addNewTabListener(ctx)
defer cancel()

//设置cookie
//获取cookie
u, _ := url.Parse("http://jd.com")
cookies := common.CookieJar.Cookies(u)

//商品链接
good_url,_:=cmd.Flags().GetString("good_url")

var res []byte
err = chromedp.Run(ctx,
chromedp.Tasks{
chromedp.ActionFunc(func(ctx context.Context) error {
//设置cookie
expr := cdp.TimeSinceEpoch(time.Now().Add(180 * 24 * time.Hour))
for _, cookie := range cookies {
_, _ = network.SetCookie(cookie.Name, cookie.Value).
network.SetCookie(cookie.Name, cookie.Value).
WithExpires(&expr).
WithPath("/").
WithDomain("." + cookie.Domain).
Expand All @@ -73,33 +76,18 @@ func startJdTdudfp(cmd *cobra.Command, args []string) {
return nil
}),
},
)
if err != nil {
log.Println("设置cookie出错了")
log.Fatal(err)
}

RETRY:
retryTimes--
log.Println("【重要提醒】自动获取eid和fp期间,建议鼠标跟随页面跳转,滑动到【加入购物车】【去购车结算】【去结算】按钮,但不要点击,可以提升获取成功率!")
log.Println(fmt.Sprintf("开始自动获取eid和fp,如遇卡住请耐心等待,重试次数剩余: %v 次", retryTimes))
var res []byte
testSkuId := common.Config.MustValue("config", "test_sku_id", "")
err = chromedp.Run(ctx,
chromedp.Tasks{
chromedp.Navigate(fmt.Sprintf("http://item.jd.com/%s.html", testSkuId)),
chromedp.WaitVisible("#InitCartUrl"), //加入购物车
chromedp.Sleep(2 * time.Second),
chromedp.Click("#InitCartUrl"),
chromedp.WaitVisible(".btn-addtocart"), //去购车结算
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.Sleep(3 * time.Second),
chromedp.Evaluate("_JdTdudfp", &res),
},
chromedp.Navigate(good_url),
chromedp.WaitVisible("#InitCartUrl"), //加入购物车
chromedp.Sleep(2 * time.Second),
chromedp.Click("#InitCartUrl"),
chromedp.WaitVisible(".btn-addtocart"), //去购车结算
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.Sleep(3 * time.Second),
chromedp.Evaluate("_JdTdudfp", &res),
)
if err != nil {
log.Println("chromedp 出错了")
Expand All @@ -109,9 +97,6 @@ func startJdTdudfp(cmd *cobra.Command, args []string) {
value := string(res)
if !gjson.Valid(value) || gjson.Get(value, "eid").String() == "" || gjson.Get(value, "fp").String() == "" {
log.Println("获取失败,请重新尝试,返回信息:" + value)
if retryTimes > 0 {
goto RETRY
}
} else {
eid := gjson.Get(value, "eid").String()
fp := gjson.Get(value, "fp").String()
Expand All @@ -137,9 +122,3 @@ func startJdTdudfp(cmd *cobra.Command, args []string) {

}
}

func addNewTabListener(ctx context.Context) <-chan target.ID {
return chromedp.WaitNewTarget(ctx, func(info *target.Info) bool {
return true
})
}

0 comments on commit 4bf0645

Please sign in to comment.