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

如何在Google Apps Script中使用Axios?遇返回Null问题求助

解决Google Apps Script中Axios无法正常调用网页抓取API的问题

问题根源

Axios是为浏览器或Node.js环境设计的HTTP客户端,Google Apps Script(GAS)的运行环境存在特殊限制,直接导入Axios的浏览器压缩包会因环境不兼容导致请求失败,返回Null。GAS本身提供了原生的UrlFetchApp服务,更适配其运行环境,适合用来发起HTTP请求。

修复方案:使用UrlFetchApp替代Axios

将原有的Axios代码替换为GAS原生的UrlFetchApp实现,以下是修正后的完整代码:

function scrapeCompanyLinkedIn() {
  const companyName = 'Company Name';
  const query = `site:linkedin.com/company "${companyName}"`;
  const apiUrl = 'https://scraperbox.com/api/scrape';
  const token = "223E9D905C2BC87EBD5181677A8C240A";

  // 构造请求参数(需根据Scraperbox API要求调整传递方式)
  const requestUrl = `${apiUrl}?token=${token}&url=${encodeURIComponent(`https://www.google.com/search?q=${encodeURIComponent(query)}`)}`;
  
  const options = {
    method: 'GET',
    muteHttpExceptions: true // 开启后可捕获HTTP错误详情,便于调试
  };

  try {
    const response = UrlFetchApp.fetch(requestUrl, options);
    const responseData = JSON.parse(response.getContentText());
    Logger.log(responseData);
  } catch (error) {
    Logger.log('请求失败:', error.toString());
    // 打印错误响应内容(如果有)
    if (error.response) {
      Logger.log('错误响应详情:', error.response.getContentText());
    }
  }
}

关键注意事项

  • 参数传递方式:Scraperbox API通常要求将token和url作为GET参数拼接在请求URL中,而非Axios式的options参数,需按照API文档调整参数传递逻辑。
  • 权限授权:首次运行代码时,需授权GAS项目使用UrlFetchApp服务,否则会因权限不足导致请求失败。
  • 调试优化:开启muteHttpExceptions: true可以获取完整的HTTP错误信息(如状态码、错误提示),帮助快速定位问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 22:07:36