Puppeteer get request redirects

蹲街弑〆低调 提交于 2020-01-13 19:27:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!