Puppeteer 获取请求重定向

编程入门 行业动态 更新时间:2024-10-12 14:22:12
本文介绍了Puppeteer 获取请求重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有什么方法可以使用 puppeteer 来获取请求的响应正文(如果有)的重定向?

Is there any way to use puppeteer to get the redirects with the response body (if there are any) of the request?

我实现了以下代码,但找不到获得重定向的方法...

I implemented the following code but I can't find a way to get the redirects...

const page = await browser.newPage() page.on('request', (data) => console.log(data)); await page.on('response', response => { const url = response.url(); response.buffer() .then ( buffer => { bufferString = buffer.toString(); }, error => { console.log(error) } ) }) await page.goto('www.ford', {waitUntil: 'networkidle0'});

推荐答案

只需在 response 处理程序中检查 response.status() - 重定向将是 3xx:

Just check response.status() in your response handler - it will be 3xx for redirects:

page.on('response', response => { const status = response.status() if ((status >= 300) && (status <= 399)) { console.log('Redirect from', response.url(), 'to', response.headers()['location']) } })

(重定向通常在响应正文中没有任何有趣的内容,因此您可能不想为它们调用 response.buffer().)

(Redirects don't usually have anything interesting in the response body, so you don't probably want to call response.buffer() for them.)

更多推荐

Puppeteer 获取请求重定向

本文发布于:2023-07-14 20:04:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106704.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   Puppeteer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!