Chromedp发送带有cookie的请求



我试图用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)
// 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, ":"+v.Value)
cookiesParam = append(cookiesParam, &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)
}

编辑:我尝试了@zachyung给出的例子,但当我试图发送任何类型的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() {
// create context
ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()
// run task list
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 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("length of cookies must be divisible by 2")
}
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 < 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
}),
// 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("chrome cookie %d: %+v", i, cookie)
}
return nil
}),
}
}

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)
}
}

我遇到了同样的问题,我添加了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
}),

相关内容

  • 没有找到相关文章

最新更新