How to trigger an excel file download in chrome from Matlab. (403 with message "Forbidden" )

4 次查看(过去 30 天)
Hello Dear Matlabers!
I coded a pretty simple code for downloading excel-files from the US Securities and Exchange Commission online site EDGAR (sec.gov) some time ago and it worked just fine. It went over thousends of files waiting 10 seconds each time as requested from the web site. I used the function
websave(filename,url);
The code:
url=append('https://www.sec.gov/Archives/edgar/data/',string(Q_10(b).Cat.EDGAR(i,1).cik),'/',string(erase(M(x),'-')),'/Financial_Report.xlsx');
try
filename = append('',string(Q_10(b).Cat.IDName(i)),'_',string(erase(M(x),'-')),'.xlsx');
websave(filename,url);
x=x+1;
pause(1/10); %Current guidelines limit each user to a total of no more than 10 requests per second%
catch
After some months I realized I had to many files and wanted to download only some of the files. (don`t aks why i need them to download again). Now the function does not seam to work any more. I get:
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://www.sec.gov/Archives/edgar/data/831259/000083125923000037/Financial_Report.xlsx.
Error in websave (line 107)
copyContentToFile(connection, filename);
The funny thing is that when I use the url on my Chrome browser:
url = "https://www.sec.gov/Archives/edgar/data/831259/000083125923000037/Financial_Report.xlsx" .
It works just fine and the files gets downloaded. I have tried to fix the problem for months now. And I have lost hope.... =(
I even tried to fix the problem with ChatGPT and downloaded selenium and java toolbox to try to configure the ChromeOptions from matlab but I can't get it to run. I am using a MacBook Pro with Apple M1 chip. (please spare me the jokes on mac) and a MATLAB R2021b home use licenze. I have not renewed the "Software Maintenance" and can't seem to add the toolboxes (selenium-java-4.27.0 & chromedriver-mac-arm64) in the Add-On Explorer. Think my promblem is much easier to fix than adding toolboxes specially if I am able to trigger the automatic download when typing the url directly on chrome. thank you for your help!! best regards Ernesto
not that I want to use this toolboxes but this is my code for that:
cd '/Users/....../MathWorks/MATLAB Add-Ons/Toolboxes/selenium-java-4.27.0'
% Import the required packages
import org.openqa.selenium.chrome.ChromeDriver.*
import org.openqa.selenium.chrome.ChromeOptions.*
import org.openqa.selenium.remote.DesiredCapabilities.*
% Specify the path to ChromeDriver
chromeDriverPath = '/Users/mondragon/Library/Application Support/MathWorks/MATLAB Add-Ons/Toolboxes/chromedriver-mac-arm64'
javaaddpath('/Users/mondragon/Library/Application Support/MathWorks/MATLAB Add-Ons/Toolboxes/selenium-java-4.27.0');
java.lang.System.setProperty('webdriver.chrome.driver', chromeDriverPath);
cd '/Users/mondragon/Library/Application Support/MathWorks/MATLAB Add-Ons/Toolboxes/chromedriver-mac-arm64'
% Create ChromeOptions to configure Chrome
ChromeOptions()
prefs = java.util.HashMap();
prefs.put('download.default_directory', 'C:\\your\\desired\\path'); % Set your desired download directory here
chromeOptions.setExperimentalOption('prefs', prefs);
% Initialize ChromeDriver with options
driver = ChromeDriver(chromeOptions);
result:
Unrecognized function or variable 'ChromeOptions'.

回答(1 个)

Divyajyoti Nayak
Divyajyoti Nayak 2024-12-22
编辑:Divyajyoti Nayak 2024-12-22
The status code given in the error is 403 which means that the request was recognized by the server but it was denied due to lack of authorization. This change is most likely done by the people handling the website. Since you are able to dowload directly from the browser, a workaround could be to open the URL in the browser via the 'web' function and then rename the files accordingly, with the 'movefile' function

类别

Help CenterFile Exchange 中查找有关 Downloads 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by