请帮助我修复代码,该代码部分摘自下面的书。它给出了一个错误“服务器响应URL https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=%EF%BF%BF&period2=%EF%BF%BF&interval=1devents=history&crumb=GAAvT/4ue2V的请求,返回状态401,消息为” Unauthorized”。”
非常感谢。
function [date, open, high, low, close, vol, adjclose]= downloadyahoo(symbol, period, start_date, end_date)
% download the data from finance.yahoo.com
% period can be '1d' for daily data, '1wk' for weekly data, '1mo' for monthly
% data
origDate = datenum('01-Jan-1970 00:00:00', 'dd-mmm-yyyy HH:MM:SS');
if ischar(start_date)
startDate = (datenum(start_date, 'ddmmyyyy') - origDate) * 24 * 60 * 60;
else
startDate = (floor(start_date) - origDate) * 24 * 60 * 60;
end
if ischar(end_date)
endDate = (datenum(end_date, 'ddmmyyyy') - origDate) * 24 * 60 * 60;
else
endDate = (floor(end_date) - origDate) * 24 * 60 * 60;
end
% Create URL string and download csv file
url_string = ['https://query1.finance.yahoo.com/v7/finance/download/' upper(symbol) '?period1=' startDate '&period2=' end_date '&interval=' period '&events=history&crumb=GAAvT/4ue2V'];
urlwrite(url_string,['data_' upper(symbol) '.csv']);
import=importdata(['data_' upper(symbol) '.csv']);
delete(['data_' upper(symbol) '.csv']);
% Reverse to normal chronological order, so 1st entry is oldest data point
open = flipud(import.data(:,1));
high = flipud(import.data(:,2));
low = flipud(import.data(:,3));
close = flipud(import.data(:,4));
vol = flipud(import.data(:,5));
adjclose = flipud(import.data(:,6));
for a=length(open):-1:1
date(length(open)-a+1)=datenum((import.textdata{a+1}),'yyyy-mm-dd');
end
end