本文介绍了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 获取请求重定向
发布评论