微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Google Apps 脚本:UrlFetchApp 的 403 错误 示例脚本:参考:

如何解决Google Apps 脚本:UrlFetchApp 的 403 错误 示例脚本:参考:

我正在使用谷歌应用程序脚本

var RSS = "https://www.sec.gov/cgi-bin/browse-edgar?action=getcurrent&CIK=&type=8-k&company=&dateb=&owner=exclude&start=0&count=100&output=atom"
var r = UrlFetchApp.fetch(RSS).getContentText()

但有一段时间,我的执行失败了,这就是响应。这是大约一半的时间。

Exception: Request Failed for https://www.sec.gov returned code 403. Truncated server response: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w... (use muteHttpExceptions option to examine full response)

不知道为什么会发生这种情况以及如何解决

解决方法

虽然很遗憾,我无法复制您的情况,例如从 but once a while,I get a failed execution and this is the response. 重新尝试请求如何?

示例脚本:

var res = null;
var maxRetries = 5;
var rss = "https://www.sec.gov/cgi-bin/browse-edgar?action=getcurrent&CIK=&type=8-k&company=&dateb=&owner=exclude&start=0&count=100&output=atom";
for (var i = 0; i < maxRetries; i++) {
  res = UrlFetchApp.fetch(rss,{muteHttpExceptions: true});
  if (res.getResponseCode() == 200) break;
  res = null;
  Utilities.sleep(5000);
}
if (!res) throw new Error("Values cannot be retrieved.");

// do something.
var value = res.getContentText();
  • 在此示例脚本中,当状态代码不是 200 时,5 秒后重试请求。并且最大重试次数为 5 次。
  • 请根据实际情况修改maxRetriesUtilities.sleep(5000)

参考:

,

当您的请求率超过“合理使用”定义(目前为每秒 10 个请求)时,就会发生这种情况。

https://www.sec.gov/os/accessing-edgar-data

您可以通过检查响应正文来确认是否是这种情况。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。