Cookies UrlFetchApp未连接到公共URL
我正在尝试使用Actions>download>CSV下的download CSV链接从此网站提取数据 我在UrlFetchApp行中始终出现错误302,我不明白为什么。我运行带有httpexception的日志程序,得到的错误是它无法连接到服务器。该网站是公开的,无论网络如何,都可以访问,所以我很难理解到底发生了什么。手动输入时,URL似乎下载CSV,但我无法让UrlFetchApp获取它Cookies UrlFetchApp未连接到公共URL,cookies,google-apps-script,http-status-code-302,urlfetch,Cookies,Google Apps Script,Http Status Code 302,Urlfetch,我正在尝试使用Actions>download>CSV下的download CSV链接从此网站提取数据 我在UrlFetchApp行中始终出现错误302,我不明白为什么。我运行带有httpexception的日志程序,得到的错误是它无法连接到服务器。该网站是公开的,无论网络如何,都可以访问,所以我很难理解到底发生了什么。手动输入时,URL似乎下载CSV,但我无法让UrlFetchApp获取它 var EquipUrl = "https://app.udot.utah.gov/apex/eom/
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);
任何帮助都将不胜感激
我只是在评论中根据建议尝试了以下内容,并得到了相同的错误302结果。我还尝试将重定向设置为false,这将以一个空的数据数组继续和结束
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);
感谢迄今为止的帮助!我在评论中说,代码在var cookies行中抛出了一个错误,但该错误已自行纠正,因此我认为这可能是您代码的一个坏副本。现在的问题是,最后一行记录器抛出了一个“无法解析文本”错误
我尝试了一些不同的选择,比如之前尝试登录,看看是否能找到任何东西。我是否可以从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
来维护状态、会话和完成请求。由于urlFetch
在服务器端运行,而不是在浏览器中运行,因此不会跨请求维护cookie。但是,我们可以在第一个请求中手动获取cookie,并在后续请求中发送它
代码段:
参考资料:
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
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
}