亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《Chromedp 发送带有 cookie 的请求》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下,希望所有认真读完的童鞋们,都有实质性的提高。
我试图使用 chromedp 库打开结帐页面,但它没有收到我发送的 cookie。我在循环中尝试了 network.setcookies()and 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)
}
编辑:我尝试了@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() {
// 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)
}
}
我遇到了同样的问题,我添加了网址。
您可以添加错误检查以查看会发生什么。
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
}),文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《Chromedp 发送带有 cookie 的请求》文章吧,也可关注golang学习网公众号了解相关技术文章。
本文内容来源于互联网,如有侵权请联系删除。
