Websave / webread : permission denied

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)

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

Hi and thanks a lot for your answer,
I have the same issue with your code ! When I try to run it I have :
Error using webread (line 122)
The error "Permission denied" occurred while communicating with URL
'https://www.google.com/finance/quote/NVDA:NYSE'.
i don't know a lot web functions as webread(), but I see that if I try any url with it I have the same issue, as with this simple test :
webread('https://www.google.fr/')
In an other hand, the function web() works pretty well.
Do you think I must modify my post to concentrate on the more general problem that I have a permission denied with any url for function type webread() ? I didn't find nothing like this on the web.
Regarding pure financial information download, thanks again for your code!
I'm not sure. It works beautifully for me. You might have to call tech support if you have that problem with every single web site you try to use webread() on. Maybe it's some kind of Firewall issue on your computer where it slaps down any unexpected program that tries to communicate over http.
I think I might have it do this for all the stocks I invest in and then use ActiveX to transfer them to an Excel workbook that I use to keep track of my portfolio.
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.
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.

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

Products

Release

R2018a

Community Treasure Hunt

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

Start Hunting!