当运行 Google AppScript 检查 url 状态但结果记录为 403 时该怎么办?

What to do when run Google AppScript to check url status but results recorded 403?

提问人:killadoop 提问时间:10/4/2023 最后编辑:TheMasterkilladoop 更新时间:10/4/2023 访问量:25

问:

我使用这个函数,它适用于大多数 URL,但对于某些 URL,B 列中的结果显示 403。我不确定这是否与用户代理有关,所以我试图强制成为特定的用户代理。但结果仍然是403。

function checkUrlsAndRecordResult() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheet1");
  var data = sheet.getRange("A2:A").getValues(); // Assuming URLs are in column B
  var resultColumn = sheet.getRange("B2:B"); // Column B to record results
  var results = [];

  // Define a specific Chrome User-Agent
  var chromeUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36";

  var headers = {
    "User-Agent": chromeUserAgent
  };

  var options = {
    "headers": headers,
    "muteHttpExceptions": true
  };

  for (var i = 0; i < data.length; i++) {
    var url = data[i][0];
    if (url) {
      try {
        var response = UrlFetchApp.fetch(url, options); // Fetch with custom headers and options
        var responseCode = response.getResponseCode();

        // Record the result in the results array
        results.push([responseCode]);

        // You can add additional checks or error handling here if needed
      } catch (e) {
        // Handle the error gracefully
        results.push(["Error"]);
      }
    } else {
      // If there's no URL in the cell, record a blank result
      results.push(['']);
    }
  }

  // Set the results in column B
  resultColumn.setValues(results);
}
google-apps-script http-status-code-403 urlfetch

评论

0赞 Serenity 10/5/2023
我也尝试在我这边复制一些 URL 并获得 403。HTTP 403 禁止访问响应状态代码指示服务器理解请求,但拒绝授权。这些网站可能不允许谷歌端(谷歌应用脚本)访问它。你试过使用其他语言吗?

答: 暂无答案