Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google chrome 如何使用chromedp获取HTTP响应正文?_Google Chrome_Http_Go_Browser Automation_Chromedp - Fatal编程技术网

Google chrome 如何使用chromedp获取HTTP响应正文?

Google chrome 如何使用chromedp获取HTTP响应正文?,google-chrome,http,go,browser-automation,chromedp,Google Chrome,Http,Go,Browser Automation,Chromedp,使用一个go软件包来驱动使用Chrome调试协议的web浏览器,我可以导航到网页、更新表单和提交表单,但我需要检索HTTP响应正文,还没有弄清楚如何进行。我希望能够检索JSON响应(而不是HTML)的HTTP响应体 从代码中查看,HTTP响应主体似乎位于CachedResponse.body属性中: 并应可通过以下方式访问: func (p *RequestCachedResponseParams) Do(ctxt context.Context, h cdp.Handler) (respon

使用一个
go
软件包来驱动使用Chrome调试协议的web浏览器,我可以导航到网页、更新表单和提交表单,但我需要检索HTTP响应正文,还没有弄清楚如何进行。我希望能够检索JSON响应(而不是HTML)的HTTP响应体

从代码中查看,HTTP响应主体似乎位于
CachedResponse.body
属性中:

并应可通过以下方式访问:

func (p *RequestCachedResponseParams) Do(ctxt context.Context, h cdp.Handler) (response *CachedResponse, err error)

示例使用了
cdp.Tasks
,例如下面这个简单示例中的任务

func googleSearch(q, text string, site, res *string) cdp.Tasks {
    var buf []byte
    sel := fmt.Sprintf(`//a[text()[contains(., '%s')]]`, text)
    return cdp.Tasks{
        cdp.Navigate(`https://www.google.com`),
        cdp.Sleep(2 * time.Second),
        cdp.WaitVisible(`#hplogo`, cdp.ByID),
        cdp.SendKeys(`#lst-ib`, q+"\n", cdp.ByID),
        cdp.WaitVisible(`#res`, cdp.ByID),
        cdp.Text(sel, res),
        cdp.Click(sel),
        cdp.Sleep(2 * time.Second),
        cdp.WaitVisible(`#footer`, cdp.ByQuery),
        cdp.WaitNotVisible(`div.v-middle > div.la-ball-clip-rotate`, cdp.ByQuery),
        cdp.Location(site),
        cdp.Screenshot(`#testimonials`, &buf, cdp.ByID),
        cdp.ActionFunc(func(context.Context, cdptypes.Handler) error {
            return ioutil.WriteFile("testimonials.png", buf, 0644)
        }),
    }
}

似乎可以通过调用
RequestCachedResponseParams.Do()
通过引用
RequestCachedResponseParams.CacheID
来访问
CachedResponse.Body
,但仍然需要以下内容:

  • 如何在
    cdp.Tasks
    中调用
    RequestCachedResponseParams.Do()
    -似乎可以使用
    cdp.ActionFunc()
  • 如何访问
    RequestCachedResponseParams.CacheID

  • 如果你想得到请求-响应,我就是这样做的

    此示例调用并侦听
    EventResponseReceived
    ,以保留包含标题的响应

    package main
    
    import (
        "context"
        "io/ioutil"
        "log"
        "os"
        "time"
    
        "github.com/chromedp/cdproto/network"
        "github.com/chromedp/chromedp"
    )
    
    func main() {
        dir, err := ioutil.TempDir("", "chromedp-example")
        if err != nil {
            panic(err)
        }
        defer os.RemoveAll(dir)
    
        opts := append(chromedp.DefaultExecAllocatorOptions[:],
            chromedp.DisableGPU,
            chromedp.NoDefaultBrowserCheck,
            chromedp.Flag("headless", false),
            chromedp.Flag("ignore-certificate-errors", true),
            chromedp.Flag("window-size", "50,400"),
            chromedp.UserDataDir(dir),
        )
    
        allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
        defer cancel()
    
        // also set up a custom logger
        taskCtx, cancel := chromedp.NewContext(allocCtx, chromedp.WithLogf(log.Printf))
        defer cancel()
    
        // create a timeout
        taskCtx, cancel = context.WithTimeout(taskCtx, 10*time.Second)
        defer cancel()
    
        // ensure that the browser process is started
        if err := chromedp.Run(taskCtx); err != nil {
            panic(err)
        }
    
        // listen network event
        listenForNetworkEvent(taskCtx)
    
        chromedp.Run(taskCtx,
            network.Enable(),
            chromedp.Navigate(`http://www.google.com`),
            chromedp.WaitVisible(`body`, chromedp.BySearch),
        )
    
    }
    
    func listenForNetworkEvent(ctx context.Context) {
        chromedp.ListenTarget(ctx, func(ev interface{}) {
            switch ev := ev.(type) {
    
            case *network.EventResponseReceived:
                resp := ev.Response
                if len(resp.Headers) != 0 {
                    log.Printf("received headers: %s", resp.Headers)
    
                }
    
            }
            // other needed network Event
        })
    }
    

    您好,我们可以为这个问题找到解决方案吗?有没有办法从listenForNeworkEvent函数读取响应正文?与服务器响应的实际内容一样,我有一个脚本,它向返回javascript代码的端点发出请求,但我看不到响应项ID,在这里找到它: