使用Chromedp发送带有cookies的请求

huangapple go评论96阅读模式
英文:

Chromedp sending request with cookies

问题

我正在尝试使用chromedp库打开一个结账页面,但它没有接收到我发送的cookie。我尝试了在循环中使用network.SetCookies()network.SetCookie(),但没有起作用。
它可以编译和运行而没有错误。希望能得到帮助,以下是代码:

opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Flag("headless", false))
actx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
ctx, cancel := chromedp.NewContext(actx)

// 在某个条件下调用cancel()来关闭Chrome。
if false {
    cancel()
}

task := chromedp.Tasks{
    network.Enable(),
    chromedp.ActionFunc(func(ctx context.Context) error {
        cookieJar := getCookies(client)
        var cookiesParam []*network.CookieParam
        for _, v := range cookieJar {
            fmt.Println(v.Name, ":"+v.Value)
            cookiesParam = append(cookiesParam, &network.CookieParam{Name: v.Name, Value: v.Value})
        }
        network.SetCookies(cookiesParam)
        return nil
    }),
    chromedp.Navigate(res.Request.URL.String()),
}

// 运行任务。
err := chromedp.Run(ctx, task)
if err != nil {
    log.Fatal(err)
}

编辑:我尝试了@zachyoung给出的示例,但当我尝试发送任何类型的cookie时,它不起作用。以下是代码:

// Command cookie is a chromedp example demonstrating how to set a HTTP cookie
// on requests.
package main

import (
    "context"
    "log"
    "time"

    "github.com/chromedp/cdproto/cdp"
    "github.com/chromedp/cdproto/network"
    "github.com/chromedp/chromedp"
)

func main() {

    // 创建上下文
    ctx, cancel := chromedp.NewContext(context.Background())
    defer cancel()

    // 运行任务列表
    var res string
    err := chromedp.Run(ctx, setcookies("https://en.afew-store.com/", &res,
        "cookie1", "value1",
        "cookie2", "value2",
    ))
    if err != nil {
        log.Fatal(err)
    }
    log.Printf("chrome received cookies: %s", res)
}

// setcookies返回一个任务,用于在网络请求中设置传递的cookie并导航到指定的主机。
func setcookies(host string, res *string, cookies ...string) chromedp.Tasks {
    if len(cookies)%2 != 0 {
        panic("cookies的长度必须是2的倍数")
    }
    return chromedp.Tasks{
        chromedp.ActionFunc(func(ctx context.Context) error {
            // 创建cookie过期时间
            expr := cdp.TimeSinceEpoch(time.Now().Add(180 * 24 * time.Hour))
            // 将cookie添加到Chrome中
            for i := 0; i < len(cookies); i += 2 {
                err := network.SetCookie(cookies[i], cookies[i+1]).
                    WithExpires(&expr).
                    WithDomain("https://en.afew-store.com/").
                    WithHTTPOnly(true).
                    Do(ctx)
                if err != nil {
                    return err
                }
            }
            return nil
        }),
        // 导航到网站
        chromedp.Navigate(host),
        // 读取返回的值
        chromedp.Text(`#result`, res, chromedp.ByID, chromedp.NodeVisible),
        // 读取网络值
        chromedp.ActionFunc(func(ctx context.Context) error {
            cookies, err := network.GetAllCookies().Do(ctx)
            if err != nil {
                return err
            }

            for i, cookie := range cookies {
                log.Printf("chrome cookie %d: %+v", i, cookie)
            }

            return nil
        }),
    }
}
英文:

I was trying to open a checkout page with chromedp library but it doesn't receive the cookies I'm sending. I tried network.SetCookies()and network.SetCookie() in a loop but it doesn't work.
It compiles and run without errors. Help is appreciated, here's the code:

opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Flag(&quot;headless&quot;, false))
actx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
ctx, cancel := chromedp.NewContext(actx)
// Call cancel() to close Chrome on some condition.
if false {
cancel()
}
task := chromedp.Tasks{
network.Enable(),
chromedp.ActionFunc(func(ctx context.Context) error {
cookieJar := getCookies(client)
var cookiesParam []*network.CookieParam
for _, v := range cookieJar {
fmt.Println(v.Name, &quot;:&quot;+v.Value)
cookiesParam = append(cookiesParam, &amp;network.CookieParam{Name: v.Name, Value: v.Value})
}
network.SetCookies(cookiesParam)
return nil
}),
chromedp.Navigate(res.Request.URL.String()),
}
// Run task.
err := chromedp.Run(ctx, task)
if err != nil {
log.Fatal(err)
}

EDIT: I tried the example given by @zachyoung but when I try to send any kind of cookie, it doesn't work. Here's the code:

// Command cookie is a chromedp example demonstrating how to set a HTTP cookie
// on requests.
package main
import (
&quot;context&quot;
&quot;log&quot;
&quot;time&quot;
&quot;github.com/chromedp/cdproto/cdp&quot;
&quot;github.com/chromedp/cdproto/network&quot;
&quot;github.com/chromedp/chromedp&quot;
)
func main() {
// create context
ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()
// run task list
var res string
err := chromedp.Run(ctx, setcookies(&quot;https://en.afew-store.com/&quot;, &amp;res,
&quot;cookie1&quot;, &quot;value1&quot;,
&quot;cookie2&quot;, &quot;value2&quot;,
))
if err != nil {
log.Fatal(err)
}
log.Printf(&quot;chrome received cookies: %s&quot;, res)
}
// setcookies returns a task to navigate to a host with the passed cookies set
// on the network request.
func setcookies(host string, res *string, cookies ...string) chromedp.Tasks {
if len(cookies)%2 != 0 {
panic(&quot;length of cookies must be divisible by 2&quot;)
}
return chromedp.Tasks{
chromedp.ActionFunc(func(ctx context.Context) error {
// create cookie expiration
expr := cdp.TimeSinceEpoch(time.Now().Add(180 * 24 * time.Hour))
// add cookies to chrome
for i := 0; i &lt; len(cookies); i += 2 {
err := network.SetCookie(cookies[i], cookies[i+1]).
WithExpires(&amp;expr).
WithDomain(&quot;https://en.afew-store.com/&quot;).
WithHTTPOnly(true).
Do(ctx)
if err != nil {
return err
}
}
return nil
}),
// navigate to site
chromedp.Navigate(host),
// read the returned values
chromedp.Text(`#result`, res, chromedp.ByID, chromedp.NodeVisible),
// read network values
chromedp.ActionFunc(func(ctx context.Context) error {
cookies, err := network.GetAllCookies().Do(ctx)
if err != nil {
return err
}
for i, cookie := range cookies {
log.Printf(&quot;chrome cookie %d: %+v&quot;, i, cookie)
}
return nil
}),
}
}

答案1

得分: 1

https://en.afew-store.com/ 不是一个有效的域名,你应该将其替换为 en.afew-store.com

- WithDomain("https://en.afew-store.com/").
+ WithDomain("en.afew-store.com").

并且页面上没有一个 #result 元素,所以 chromedp.Text("#result", res, chromedp.ByID, chromedp.NodeVisible) 永远不会返回结果。这是一个修改后可以工作的示例:

package main

import (
	"context"
	"log"
	"time"

	"github.com/chromedp/cdproto/cdp"
	"github.com/chromedp/cdproto/network"
	"github.com/chromedp/cdproto/storage"
	"github.com/chromedp/chromedp"
)

func main() {
	ctx, cancel := chromedp.NewContext(context.Background())
	defer cancel()

	err := chromedp.Run(ctx,
		chromedp.ActionFunc(func(ctx context.Context) error {
			expr := cdp.TimeSinceEpoch(time.Now().Add(180 * 24 * time.Hour))
			cookies := []string{"cookie1", "value1", "cookie2", "value2"}
			for i := 0; i < len(cookies); i += 2 {
				err := network.SetCookie(cookies[i], cookies[i+1]).
					WithExpires(&expr).
					WithDomain("en.afew-store.com").
					WithHTTPOnly(true).
					Do(ctx)
				if err != nil {
					return err
				}
			}
			return nil
		}),
		chromedp.Navigate("https://en.afew-store.com/"),
		chromedp.ActionFunc(func(ctx context.Context) error {
			cookies, err := storage.GetCookies().Do(ctx)
			if err != nil {
				return err
			}

			for i, cookie := range cookies {
				log.Printf("chrome cookie %d: %+v", i, cookie)
			}

			return nil
		}),
	)
	if err != nil {
		log.Fatal(err)
	}
}
英文:

https://en.afew-store.com/ is not a domain, you should replace it with en.afew-store.com.

- WithDomain(&quot;https://en.afew-store.com/&quot;).
+ WithDomain(&quot;en.afew-store.com&quot;).

And there is not an #result element on the page, so chromedp.Text(&quot;#result&quot;, res, chromedp.ByID, chromedp.NodeVisible) never returns. Here is a modified demo that works:

package main

import (
	&quot;context&quot;
	&quot;log&quot;
	&quot;time&quot;

	&quot;github.com/chromedp/cdproto/cdp&quot;
	&quot;github.com/chromedp/cdproto/network&quot;
	&quot;github.com/chromedp/cdproto/storage&quot;
	&quot;github.com/chromedp/chromedp&quot;
)

func main() {
	ctx, cancel := chromedp.NewContext(context.Background())
	defer cancel()

	err := chromedp.Run(ctx,
		chromedp.ActionFunc(func(ctx context.Context) error {
			expr := cdp.TimeSinceEpoch(time.Now().Add(180 * 24 * time.Hour))
			cookies := []string{&quot;cookie1&quot;, &quot;value1&quot;, &quot;cookie2&quot;, &quot;value2&quot;}
			for i := 0; i &lt; len(cookies); i += 2 {
				err := network.SetCookie(cookies[i], cookies[i+1]).
					WithExpires(&amp;expr).
					WithDomain(&quot;en.afew-store.com&quot;).
					WithHTTPOnly(true).
					Do(ctx)
				if err != nil {
					return err
				}
			}
			return nil
		}),
		chromedp.Navigate(&quot;https://en.afew-store.com/&quot;),
		chromedp.ActionFunc(func(ctx context.Context) error {
			cookies, err := storage.GetCookies().Do(ctx)
			if err != nil {
				return err
			}

			for i, cookie := range cookies {
				log.Printf(&quot;chrome cookie %d: %+v&quot;, i, cookie)
			}

			return nil
		}),
	)
	if err != nil {
		log.Fatal(err)
	}
}

答案2

得分: 0

我遇到了同样的问题,我添加了URL。

你可以添加一个错误检查来查看发生了什么。

cdp.ActionFunc(func(ctx context.Context) error {
for _, cookie := range cookies {
err := network.SetCookie(cookie.Name, cookie.Value).
WithDomain(cookie.Domain).
WithURL(https://www.blablabla.com/blu).
Do(ctx)

	if err != nil {
panic(err)
}
}
return nil

})

英文:

I faced the same issue and I added the url.

You could add an error check to see what happens.

		cdp.ActionFunc(func(ctx context.Context) error {
for _, cookie := range cookies {
err := network.SetCookie(cookie.Name, cookie.Value).
WithDomain(cookie.Domain).
WithURL(`https://www.blablabla.com/blu`).
Do(ctx)
if err != nil {
panic(err)
}
}
return nil
}),

huangapple
  • 本文由 发表于 2022年10月17日 23:01:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/74099245.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定