【问题标题】:How to get rest API JSON response from Puppeteer? [duplicate]如何从 Puppeteer 获取 REST API JSON 响应? [复制]
【发布时间】:2019-08-18 23:24:59
【问题描述】:

我正在抓取一个网站以获取一些详细信息。我正在向一个 url 发送一个 POST 请求,并且需要为此获取 JSON 响应数据,因为我需要的数据没有呈现到 HTML 中。

通常我只会使用 axios.post() 或替代方法,但其余 api 会阻止没有会话 ID 的请求,因此我必须使用无头浏览器。

那么,有没有办法拦截 REST API 的 JSON 响应,就像您可以在开发人员工具的“网络”选项卡中检查它一样?

【问题讨论】:

标签: node.js screen-scraping puppeteer


【解决方案1】:

有了 axios,您就有了一个中间件管道,您可以使用它来拦截来自服务器的响应。这在他们的自述文件中标记为“拦截器”的部分下

https://github.com/axios/axios#interceptors

// Add a request interceptor
axios.interceptors.request.use(function (config) {
    // Do something before request is sent
    return config;
  }, function (error) {
    // Do something with request error
    return Promise.reject(error);
  });

// Add a response interceptor
axios.interceptors.response.use(function (response) {
    // Do something with response data
    return response;
  }, function (error) {
    // Do something with response error
    return Promise.reject(error);
  });

【讨论】:

  • 我没有使用 axios,因为其余的 api 用消息“需要会话”禁止 HTTP 阻止了我的请求。所以我需要使用无头浏览器。
猜你喜欢
  • 1970-01-01
  • 2019-08-18
  • 2017-01-21
  • 2019-04-25
  • 1970-01-01
  • 2015-12-26
  • 1970-01-01
  • 2018-05-31
  • 1970-01-01
相关资源
最近更新 更多