gpt4 book ai didi

Puppeteer:如何在page.goto().catch中获取httpcode?

转载 作者:行者123 更新时间:2023-12-03 08:51:21 25 4
gpt4 key购买 nike

我想打开一个 url 并收集其 httpcode 和错误,但我无法在 goto().catch 中获取其 httpcode。

let result = {};
page.goto(url)
.then(res => {
result['http-code'] = res.status();
})
.catch(err => {
result['http-code'] = '?'; // TODO: get http-code in goto().catch
result['page-error'] = err + '';
})
.then(() => {
res.json(result);
});

最佳答案

这是不可能的。

TL;DR

因为page.goto当远程服务器返回任何有效的 HTTP 状态代码时,该方法不会抛出错误,包括 404“未找到”和 500“内部服务器错误”。可以通过调用response.status()来检索此类响应的状态代码;

page.goto如果出现以下情况,将会抛出错误:

  • 存在 SSL 错误(例如自签名证书)。
  • 目标网址无效。
  • 导航期间超时。
  • 远程服务器没有响应或无法访问。主要资源加载失败。

NOTE page.goto either throws an error or returns a main resource response. The only exceptions are navigation to about:blank or navigation to the same URL with a different hash, which would succeed and return null.

关于Puppeteer:如何在page.goto().catch中获取httpcode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58722044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com