UrlFetchApp获取方法失败,出现404错误,但相同的URL适用于Curl和浏览器



如果我执行以下代码,它可以正常工作:

curl 'http://performance.morningstar.com/perform/Performance/stock/trailing-total-returns.action?&t=DAL&region=usa&culture=en_US&comparisonRemove=true&cur=&ops=clear&ep=true&align=d&annlz=true'

我也可以从任何浏览器执行这个URL,它会返回预期的结果

http://performance.morningstar.com/perform/Performance/stock/trailing-total-returns.action?&t=DAL&region=usa&culture=en_US&comparisonRemove=true&cur=&ops=clear&ep=true&align=d&annlz=true

但是,下面的代码失败,返回错误404。

function fetchFromURLTemp() {
let url = "http://performance.morningstar.com/perform/Performance/stock/trailing-total-returns.action?&t=DAL&region=usa&culture=en_US&comparisonRemove=true&cur=&ops=clear&ep=true&align=d&annlz=true";
var options = {
'method': 'GET',
'headers': { 'Accept': '*/*' },
'muteHttpExceptions': true,
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*; q = 0.8'
};
let httpResponse = UrlFetchApp.fetch(url, options);
let status = httpResponse.getResponseCode()
let urlData = httpResponse.getContentText();
console.info("Response Status : " + status + ", Response Content : " + urlData)
}

它在控制台上打印以下消息

Response Status : 404, Response Content : The report is no longer supported

有人能告诉我需要什么样的改变才能得到回应吗。

谢谢!

它对我来说很好,只需将let更改为var

function fetchFromURLTemp() {
var url = "http://performance.morningstar.com/perform/Performance/stock/trailing-total-returns.action?&t=DAL&region=usa&culture=en_US&comparisonRemove=true&cur=&ops=clear&ep=true&align=d&annlz=true";
var options = {
'method': 'GET',
'headers': { 'Accept': '*/*' },
'muteHttpExceptions': true,
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*; q = 0.8'
};
var httpResponse = UrlFetchApp.fetch(url, options);
var status = httpResponse.getResponseCode()
var urlData = httpResponse.getContentText();
Logger.log("Response Status : " + status + ", Response Content : " + urlData)
}

最新更新