问题描述
我正在使用谷歌应用程序脚本
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 次。 - 请根据实际情况修改
maxRetries
和Utilities.sleep(5000)
。
参考:
,当您的请求率超过“合理使用”定义(目前为每秒 10 个请求)时,就会发生这种情况。
https://www.sec.gov/os/accessing-edgar-data
您可以通过检查响应正文来确认是否是这种情况。