Puppeteer 获取请求重定向 [英] Puppeteer get request redirects
本文介绍了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('https://www.ford.com', {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 获取请求重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文