Websave / webread : permission denied

28 views (last 30 days)
Hi,
I'm looking for download a file from an URL. When I try the function web(url), it works but it cannot be automated because I have the "Save File As" dialog box. So I've tried websave(filename,url) but then I have the following error :
%% The error "Permission denied" occurred while communicating with URL
%% 'https://query1.finance.yahoo.com/v7/finance/download/TSLA?period1=1589414400&period2=1623110400&interval=1d&events=history&includeAdjustedClose=true'.
Then, I realize this error happen for any url link with usage of command like websave(), webread() etc...

Accepted Answer

Hugo Chavy
Hugo Chavy on 10 Jun 2021
Edited: Hugo Chavy on 10 Jun 2021
Thanks @Image Analyst !
To recap : to solve the problem of permission denied with web type functions, we shall disable the firewall -> the one's of Windows Defender for me !
Have a nice day everyone

More Answers (2)

Image Analyst
Image Analyst on 9 Jun 2021
Yeah, I get a different error "Forbidden":
Error using matlab.internal.webservices.HTTPConnector/copyContentToFile (line 389)
The server returned the status 403 with message "Forbidden" in response to the request to URL
https://query1.finance.yahoo.com/v7/finance/download/TSLA?period1=1589414400&period2=1623110400&interval=1d&events=history&includeAdjustedClose=true.
Error in websave (line 107)
copyContentToFile(connection, filename);
Error in test1 (line 11)
websave(filename,url)
Try this code I just wrote to get the price in Google Finance instead.:
% Demo by Image Analyst to ask Google Finance for today's closing stock price.
clc; % Clear command window.
fprintf('Running %s.m ...\n', mfilename);
workspace; % Make sure the workspace panel is showing.
tickerSymbol = 'NVDA'; % NVidia is on NASDAQ
price = GetStockPrice(tickerSymbol);
tickerSymbol = 'PG'; % P&G (The Procter & Gamble Company) is on NYSE
price = GetStockPrice(tickerSymbol);
fprintf('Done running %s.m\n', mfilename);
%==============================================================================
% Function to get today's price on the specified stock ticker symbol.
% By Image Analyst. June 8, 2021.
function price = GetStockPrice(tickerSymbol)
% Convert to upper case, as that is standard for stock symbols.
tickerSymbol = upper(tickerSymbol);
% Construct the URL to Google Finance.
url = sprintf('https://www.google.com/finance/quote/%s:NYSE', tickerSymbol);
% For example: url = 'https://www.google.com/finance/quote/PG:NYSE'
% Ask Google Finance for the stock quote.
webPageContents = webread(url);
% Search the returned character string for the string 'data-last-price='.
% The latest stock price will be after this.
indexes = strfind(webPageContents, 'data-last-price=');
% See if we found this ticker symbol.
if isempty(indexes)
% It's not listed on the NYSE. Try NASDAQ instead.
url = sprintf('https://www.google.com/finance/quote/%s:NASDAQ', tickerSymbol);
webPageContents = webread(url);
indexes = strfind(webPageContents, 'data-last-price=');
end
if isempty(indexes)
% Not on NYSE ro NASDAQ. Bail out.
errorMessage = sprintf('Symbol %s not found in Google Finance in either NYSE or NASDAQ echange', tickerSymbol);
uiwait(errordlg(errorMessage));
return;
end
% Search the string for the closing date. It will be indicated by "Closed:".
indexes2 = strfind(webPageContents, 'Closed:');
if ~isempty(indexes2)
for k = 1 : length(indexes2)
i1 = indexes2(k) + 8;
i2 = indexes2(k) + 24;
dateString = webPageContents(i1:i2);
end
end
% Search the string for the price. It will be between a pair of double quotes.
for k = 1 : length(indexes)
i1 = indexes(k);
i2 = indexes(k) + 25;
str = webPageContents(i1:i2);
% Find double quotes.
quoteLocations = strfind(str, '"');
% Extract the price as a string.
str = str(quoteLocations(1)+1 : quoteLocations(2) - 1);
% Convert the string to a numeric double.
price = str2double(str);
% fprintf('string %s\n', str);
% Get current year.
y = year(datetime(datestr(now)));
% Print it out to the command window (optional).
fprintf('%s Stock Price = $%.2f on %s, %g.\n', tickerSymbol, price, dateString, y);
end
end
You see:
NVDA Stock Price = $698.28 on Jun 8, 7:58:37 PM, 2021.
PG Stock Price = $134.84 on Jun 8, 6:13:49 PM, 2021.
  4 Comments
Hugo Chavy
Hugo Chavy on 10 Jun 2021
Man, the deactivation of the firewall works ! So thanks a lot for the advice AND for your exemple to take data from google finance !
I will modify the post to concentrate on the problem of the firewall to help other people who have the same problem.
Image Analyst
Image Analyst on 10 Jun 2021
Contact your IT department for a safer solution than deactivating the firewall. Maybe you can just put that one web site, or the MATLAB program, on a white list. It's probably not safe to just deactivate your protection for everything.

Sign in to comment.


Image Analyst
Image Analyst on 10 Jun 2021
Update to handle Mutual Funds:
% Demo by Image Analyst to ask Google Finance for today's closing stock price.
clc; % Clear command window.
fprintf('Running %s.m ...\n', mfilename);
workspace; % Make sure the workspace panel is showing.
tickerSymbol = 'NVDA'; % NVidia is on NASDAQ
price = GetStockPrice(tickerSymbol);
tickerSymbol = 'PG'; % P&G (Procter & Gamble) is on NYSE
price = GetStockPrice(tickerSymbol);
tickerSymbol = 'SNXFX'; % Schwab 1000 is a Mutual Fund
price = GetStockPrice(tickerSymbol);
fprintf('Done running %s.m\n', mfilename);
%==============================================================================
% Function to get today's price on the specified stock ticker symbol.
function price = GetStockPrice(tickerSymbol)
stockOrMutualFund = 1; % Set to 2 if it turns out to be a mutual fund.
% Convert to upper case, as that is standard for stock symbols.
tickerSymbol = upper(tickerSymbol);
% Construct the URL to Google Finance.
url = sprintf('https://www.google.com/finance/quote/%s:NYSE', tickerSymbol);
% For example: url = 'https://www.google.com/finance/quote/PG:NYSE'
% Ask Google Finance for the stock quote.
webPageContents = webread(url);
% Search the returned character string for the string 'data-last-price='.
% The latest stock price will be after this.
indexes = strfind(webPageContents, 'data-last-price=');
% See if we found this ticker symbol.
if isempty(indexes)
% It's not listed on the NYSE. Try NASDAQ instead.
url = sprintf('https://www.google.com/finance/quote/%s:NASDAQ', tickerSymbol);
webPageContents = webread(url);
indexes = strfind(webPageContents, 'data-last-price=');
end
if isempty(indexes)
% It's not listed on the NYSE OR NASDAQ. Maybe it's a mutual fund. Try MUTF instead.
url = sprintf('https://www.google.com/finance/quote/%s:MUTF', tickerSymbol);
webPageContents = webread(url);
indexes = strfind(webPageContents, 'data-last-price=');
if ~isempty(indexes)
stockOrMutualFund = 2; % Set to 2 if it turns out to be a mutual fund.
end
end
if isempty(indexes)
% Not on NYSE ro NASDAQ. Bail out.
errorMessage = sprintf('Symbol %s not found in Google Finance in either NYSE or NASDAQ echange', tickerSymbol);
uiwait(errordlg(errorMessage));
return;
end
% Search the string for the closing date. It will be indicated by "Closed:".
indexes2 = strfind(webPageContents, 'Closed:');
if ~isempty(indexes2) && stockOrMutualFund == 1
% Stock.
for k = 1 : length(indexes2)
i1 = indexes2(k) + 8;
i2 = indexes2(k) + 24;
dateString = webPageContents(i1:i2);
end
else
% Mutual Fund
dateString = datestr(now);
end
% Search the string for the price. It will be between a pair of double quotes.
for k = 1 : length(indexes)
i1 = indexes(k);
i2 = indexes(k) + 25;
str = webPageContents(i1:i2);
% Find double quotes.
quoteLocations = strfind(str, '"');
% Extract the price as a string.
str = str(quoteLocations(1)+1 : quoteLocations(2) - 1);
% Convert the string to a numeric double.
price = str2double(str);
% fprintf('string %s\n', str);
% Get current year.
y = year(datetime(datestr(now)));
% Print it out to the command window (optional).
fprintf('%s Stock Price = $%.2f on %s, %g.\n', tickerSymbol, price, dateString, y);
end
end

Categories

Find more on Financial Data in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!