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

调用Bol.com API获取搜索量遇400错误,求排查解决方法

问题描述

我的目标是通过Bol.com API,从Google Sheet中的关键词获取搜索量(Search Volumes)。为此我使用了以下脚本:

function getSearchVolumesFromSheet() {
  // 替换为你的Bol.com API密钥
  var apiKey = "IPUTMUAPIKEYHEREBUTREDACTEDFORPRIVACYPURPOSES";
  
  // 替换为存放关键词的工作表名称
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  
  // 从第一列获取关键词
  var searchTerms = sheet.getRange("A1:A").getValues().flat();
  
  // 调用Bol.com API获取每个关键词的搜索量并收集到数组中
  var searchVolumes = [];
  for (var i = 0; i < searchTerms.length; i++) {
    var searchTerm = searchTerms[i][0];
    var url = "https://api.bol.com/catalog/v4/search/?q=" + encodeURIComponent(searchTerm) + "&dataoutput=SEARCH_VOL_PLUS";
    var response = UrlFetchApp.fetch(url, {
      headers: {
        Authorization: "Bearer " + apiKey
      }
    });
    var data = JSON.parse(response.getContentText());
    var productSearchVolumes = data.products.map(function(product) {
      return product.search_volume;
    });
    searchVolumes.push(productSearchVolumes);
  }
  
  // 将搜索量写入工作表第二列
  sheet.getRange(1, 2, searchVolumes.length, searchVolumes[0].length).setValues(searchVolumes);
}

运行脚本时出现如下错误:

Exception: Request failed for https://api.bol.com returned code 400
getSearchVolumesFromSheet
@ Code.gs:16

查阅Bol.com API文档后仍无头绪,请问接下来可以尝试哪些解决方法?

解决建议
  • 捕获并查看API详细错误响应:400属于客户端请求错误,API通常会返回具体错误信息。修改脚本添加错误捕获,打印响应内容:

    try {
      var response = UrlFetchApp.fetch(url, {
        headers: { Authorization: "Bearer " + apiKey },
        muteHttpExceptions: true // 允许获取错误响应内容
      });
      var responseCode = response.getResponseCode();
      if (responseCode !== 200) {
        console.log("请求URL: " + url);
        console.log("响应码: " + responseCode);
        console.log("响应内容: " + response.getContentText());
        continue; // 继续处理下一个关键词
      }
      // 后续解析数据的代码保持不变
    } catch (e) {
      console.log("请求异常: " + e.toString());
    }
    

    通过日志明确错误原因,比如参数缺失、权限不足、密钥无效等。

  • 检查API密钥的有效性与权限:确认你的API密钥是否拥有SEARCH_VOL_PLUS数据输出的权限,是否在Bol.com开发者后台正确配置,密钥是否过期或格式错误(注意Bearer令牌不要有多余空格)。

  • 验证请求参数与URL格式:

    • 过滤空关键词:searchTerms[i][0]可能存在空值,导致q参数为空触发400,添加判断跳过:
      var searchTerm = searchTerms[i][0];
      if (!searchTerm) continue; // 跳过空值
      
    • 补充必要参数:Bol.com API可能需要指定市场区域(如&country=NL),确认dataoutput=SEARCH_VOL_PLUS参数的大小写和拼写是否符合要求。
  • 控制API调用频率:Bol.com API存在调用频率限制,短时间内多次请求可能触发400或429错误,在循环中添加延迟:

    Utilities.sleep(1000); // 每次请求后暂停1秒
    
  • 确认API端点正确性:检查是否使用了最新的Bol.com Catalog API端点,避免调用已废弃的路径。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 15:08:12