You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SEC EDGAR API接入Google Sheets遇403错误求解决方案

解决Google Sheets调用SEC EDGAR API返回403的问题

问题根源

SEC的EDGAR API强制要求请求头包含User-Agent字段,需明确标注请求来源(如你的姓名+邮箱),而原生ImportJSON函数默认未添加该字段,因此被服务器拒绝访问,返回403错误。

解决方法:自定义Google Apps Script函数

原生ImportJSON不支持自定义请求头,需编写脚本实现带合规请求头的API调用:

  1. 打开目标Google Sheet,点击顶部菜单栏「扩展程序」→「Apps脚本」
  2. 删除默认代码,粘贴以下脚本:
function importSECJSON(url) {
  // 替换为你的姓名和邮箱,符合SEC要求的User-Agent格式
  const options = {
    headers: {
      'User-Agent': '你的姓名 your-email@example.com'
    }
  };
  
  // 发送请求并解析JSON
  const response = UrlFetchApp.fetch(url, options);
  const jsonData = JSON.parse(response.getContentText());
  
  // 将JSON转为Sheet可识别的二维数组
  return convertJSONtoArray(jsonData);
}

// 辅助函数:把嵌套JSON转为扁平二维结构
function convertJSONtoArray(obj) {
  const result = [];
  const headers = new Set();
  
  // 收集所有键作为表头
  function collectKeys(item) {
    for (const key in item) {
      headers.add(key);
      if (typeof item[key] === 'object' && item[key] !== null && !Array.isArray(item[key])) {
        collectKeys(item[key]);
      }
    }
  }
  
  // 处理数组或单个对象的表头收集
  if (Array.isArray(obj)) {
    obj.forEach(item => collectKeys(item));
  } else {
    collectKeys(obj);
  }
  
  const headerArray = [...headers];
  result.push(headerArray);
  
  // 填充每行数据
  function fillRow(item) {
    const row = [];
    headerArray.forEach(key => {
      let value = item[key];
      // 嵌套对象转为字符串展示
      if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
        value = JSON.stringify(value);
      }
      row.push(value || '');
    });
    return row;
  }
  
  if (Array.isArray(obj)) {
    obj.forEach(item => result.push(fillRow(item)));
  } else {
    result.push(fillRow(obj));
  }
  
  return result;
}
  1. 修改脚本中的User-Agent值,格式必须为「姓名 邮箱」,例如'John Doe john.doe@example.com'
  2. 保存脚本(点击保存按钮,给项目命名如SECEDGARImporter)
  3. 返回Google Sheet,在单元格中输入公式调用:
=importSECJSON("https://data.sec.gov/api/xbrl/companyfacts/CIK0000320193.json")

(替换为你需要调用的EDGAR API接口URL)

关键注意事项

  • 严格遵守SEC API限流规则:每10秒最多10次请求,每分钟最多100次请求,避免被封禁
  • User-Agent必须真实填写,SEC会通过该字段联系违规使用者,不填会持续返回403
  • 如需提取特定JSON字段,可修改convertJSONtoArray函数逻辑,或结合原生ImportJSON的解析规则扩展

内容的提问来源于stack exchange,提问作者John Tiller

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 03:55:28