【问题标题】:UrlFetchApp not connecting to public URLUrlFetchApp 未连接到公共 URL
【发布时间】:2019-05-08 15:49:26
【问题描述】:

我正在尝试使用“操作”>“下载”>“CSV”下的 CSV 下载链接从该网站提取数据

https://app.udot.utah.gov/apex/eom/f?p=288:300

我在 UrlFetchApp 行上一直收到错误 302,但我不明白为什么。我使用 httpexception 运行记录器并收到无法连接到服务器的错误。无论网络如何,该网站都是公开且可访问的,因此我很难理解发生了什么。手动输入时,URL 似乎下载了 CSV,但我无法让 UrlFetchApp 抓取它。

var EquipUrl = "https://app.udot.utah.gov/apex/eom/f?p=288:300:::CSV";
var EquipContent = UrlFetchApp.fetch(EquipUrl).getContentText();
var EquipData = Utilities.parseCsv(EquipContent);

任何帮助将不胜感激!

我刚刚在 cmets 中尝试了以下建议,并得到了相同的错误 302 结果。我还尝试将重定向设置为 false,这会继续并以空的 EquipData 数组结束

var options = {
'followRedirects' :true
];
var EquipUrl = "https://app.udot.utah.gov/apex/eom/f?p=288:300:::CSV::::";
var EquipContent = UrlFetchApp.fetch(EquipUrl, options).getContentText();
var EquipData = Utilities.parseCsv(EquipContent);

感谢到目前为止 TheMaster 的帮助!我在评论中说代码在 var cookies 行上吐出了一个错误,但该错误自行纠正,所以我认为它可能是我的代码的错误副本。现在的问题是最后的 Logger 行吐出“无法解析文本”错误。

function fetchUrlWithCookie() {
  var url = 'https://app.udot.utah.gov/apex/eom/f?p=288:300:::CSV';
  var response = UrlFetchApp.fetch(url, {
    muteHttpExceptions: true,
    followRedirects: false,
  });
  var cookie = response.getAllHeaders()['Set-Cookie'];
  response = UrlFetchApp.fetch(url, {
    muteHttpExceptions: true,
    headers: {
      Cookie: cookie, //send the cookie we got as header
    },
  });
  Logger.log(Utilities.parseCsv(response.getContentText()));//parseCSV
}

我尝试了一些不同的选项,例如尝试登录以查看是否可以找到任何东西。我可以从 Stackdriver 日志记录中提取任何有助于识别问题的信息吗?

非常感谢大师!有效!如果您能评论并解决您的问题解决过程,我会很高兴,这样我就可以帮助了解下次发生这种情况时要注意什么,但我知道这要求很多。

  var url = 'https://app.udot.utah.gov/apex/eom/f?p=288:300::CSV::::';
  var response = UrlFetchApp.fetch(url, {
    muteHttpExceptions: true,
    followRedirects: true,
  });
  var cookie = response.getAllHeaders()['Set-Cookie']; 
  response = UrlFetchApp.fetch(url, {
    muteHttpExceptions: true,
    headers: {
      Cookie: cookie, //send the cookie we got as headerw
    },
  });
  Logger.log(Utilities.parseCsv(response.getContentText()));//parseCSV

这是一个完整功能的代码副本。再次感谢您在这几天内为解决此问题提供的帮助。

【问题讨论】:

    标签: cookies google-apps-script http-status-code-302 urlfetch


    【解决方案1】:

    要点:

    一些网站使用cookies 来维护状态、会话和完成请求。由于 urlFetch 在服务器端而不是在浏览器中运行,因此不会跨请求维护 cookie。但是,我们可以在第一个请求中手动获取 cookie,并在后续请求中发送。

    代码片段:

    function fetchUrlWithCookie() {
      var url = 'xxxx'; //Your csv url
      var response = UrlFetchApp.fetch(url, {
        muteHttpExceptions: true,
        followRedirects: false,
      });
      var cookie = response.getAllHeaders()['Set-Cookie']; //Get cookie from header 
      response = UrlFetchApp.fetch(url, {
        muteHttpExceptions: true,
        headers: {
          Cookie: cookie, //send the cookie we got as header
        },
      });
      Logger.log(Utilities.parseCsv(response.getContentText()));//parseCSV
    }
    

    参考资料:

    【讨论】:

    • 感谢您迄今为止的帮助。我实现了您建议的代码 sn-p,现在在 var cookie 行上遇到“TypeError:无法在对象中找到函数 getAllHeaders”
    • @John 编辑您的问题以包含引发错误的新代码。
    猜你喜欢
    • 2011-08-08
    • 2019-08-17
    • 2021-04-28
    • 2015-01-23
    • 2015-12-18
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多