【问题标题】:Unable to produce json response issuing get requests with parameters无法生成带有参数的 get 请求的 json 响应
【发布时间】:2023-03-30 10:29:01
【问题描述】:

我正在尝试从 webpage 获取带有适当参数的 get http 请求的 json 响应,但我得到 403 Forbidden 作为响应。当我在 python 中做同样的事情时,我会得到相应的响应。但是,当我采用以下方法时,事情就会出错:

function capterraScraper() {
  var Url = 'https://www.capterra.com/directoryPage/rest/v1/category';
  var params = {
      'htmlName': '360-degree-feedback-software',
      'rbr': 'false',
      'countryCode': 'BD'
  };
  var options = {
    'method' : 'GET',
    'params' : params,
    'headers' : {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'
    }
  };
  var response = UrlFetchApp.fetch(Url, options);
  console.log(JSON.parse(response));
}

如何让脚本产生 json 响应?

【问题讨论】:

  • 你能展示你的python脚本吗?
  • Gite this 支票@TheMaster。

标签: google-apps-script web-scraping google-sheets parameters http-status-code-403


【解决方案1】:

从您的脚本中,我认为params 可能用作查询参数。我在测试的时候确认可以获取到JSON数据。但是当我使用 Google Apps Script 测试 URL 时,我也确认了 403 Forbidden 的错误。幸运的是,当我使用 Javascript 访问 URL 时,可以获得 JSON 数据。因此,在这种情况下,作为一种解决方法,我想提出一种在 Google Apps 脚本端使用 JSON 数据的解决方法。

变通方法的流程如下。

  • 打开一个对话框。
  • 使用 Javascript 检索 JSON 数据,并将数据返回到 Google Apps 脚本端。

这样,您可以在 Google Apps Script 中使用 JSON 数据。

示例脚本:

请将以下脚本复制并粘贴到 Google 电子表格的脚本编辑器中,然后在脚本编辑器中运行 main()。这样,在 Google 电子表格中打开一个对话框并检索 JSON 数据,并将数据返回到 Google Apps 脚本端。

function main(obj) {
  if (!obj) {
    var html = HtmlService.createHtmlOutput(`<script>fetch('https://www.capterra.com/directoryPage/rest/v1/category?' + (new URLSearchParams({htmlName: '360-degree-feedback-software',rbr: 'false',countryCode: 'BD'}))).then(response => response.json()).then(res => google.script.run.withSuccessHandler(google.script.host.close).main(res)).catch(err => console.log(err));</script>`);
    SpreadsheetApp.getUi().showModalDialog(html, "sample");
    return;
  }
  console.log(obj)
  DriveApp.createFile("sample.txt", JSON.stringify(obj));
}
  • 运行此示例脚本时,检索到的 JSON 数据将保存在根文件夹中的文件中。当然,您可以将数据用作obj

  • 此脚本中的Javascript如下。

      <script>
      fetch('https://www.capterra.com/directoryPage/rest/v1/category?' + (new URLSearchParams({htmlName: '360-degree-feedback-software',rbr: 'false',countryCode: 'BD'})))
      .then(response => response.json())
      .then(res => google.script.run.withSuccessHandler(google.script.host.close).main(res))
      .catch(err => console.log(err));
      </script>
    

注意:

  • 此解决方法使用 Google 电子表格上的对话框。所以,不幸的是,在这种情况下,即使从外部运行此脚本并且是时间驱动的触发器,也无法使用该脚本。请注意这一点。

参考:

【讨论】:

  • 嗨@Tanaike,我怎样才能以正确的方式使用query parameter 来形成一个合格的网址以发送get requests,因为params=params 在谷歌应用脚​​本中似乎不正确。还有一件事,我如何在应用程序脚本中安装 axios。感谢一万亿。
  • @MITHU 感谢您的回复。我很高兴你的问题得到了解决。关于您的新问题 1,我认为我的帖子可能有用。 Ref关于您的新问题2,Google Apps Script 无法使用Axios。如果要使用它,则需要使用对话框作为我建议的解决方法。对此我深表歉意。
  • 我真的不想在这里使用 axios。我只是想学习如何安装和使用任何外部库,如 axios e.tc。在应用程序脚本中。
  • @MITHU 感谢您的回复。我不得不为我糟糕的英语水平道歉。不幸的是,Axios 不能与 Google Apps 脚本一起使用。我再次为此深表歉意。
猜你喜欢
  • 1970-01-01
  • 2014-02-15
  • 1970-01-01
  • 1970-01-01
  • 2018-04-19
  • 2013-11-23
  • 1970-01-01
  • 1970-01-01
  • 2018-07-11
相关资源
最近更新 更多