问题
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'});
回答1:
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'])
}
})
(Redirects don't usually have anything interesting in the response body, so you don't probably want to call response.buffer()
for them.)
来源:https://stackoverflow.com/questions/48986851/puppeteer-get-request-redirects