can MATLAB download files from a website ?

조회 수: 56 (최근 30일)
Christophe
Christophe 2012년 11월 16일
댓글: Marco Giangolini 2023년 11월 3일
Is it possible with MATLAB to download files from a website ? If the answer is yes, how do we proceed ?

답변 (4개)

Carlton
Carlton 2023년 6월 1일
이동: Image Analyst 2023년 6월 1일
Yes, it is possible to download files from a website using MATLAB. Here's a general approach you can follow:
  1. Use the MATLAB function webread or websave to download the file from the website. Both functions can handle HTTP and HTTPS requests.
  2. Determine the URL of the file you want to download. This could be a direct link to the file or a URL that triggers a file download.
  3. If you're using webread, you can use the following syntax to download the file:
matlab
url = 'https://www.example.com/path/to/file.txt';
data = webread(url);
  1. If you're using websave, you can specify the filename and path where the downloaded file should be saved. Here's an example:
matlab
url = 'https://www.example.com/path/to/file.txt';
filename = 'myfile.txt';
websave(filename, url);
  1. After executing the appropriate function, MATLAB will initiate the download process and save the file to the specified location. Make sure you have the necessary permissions to write files in the chosen directory.
Note: Some websites may require authentication or have restrictions on downloading files programmatically. In such cases, additional steps might be needed, such as providing login credentials or analyzing the website's API for download options.
It's important to review the terms and conditions of the website you're accessing to ensure that downloading files aligns with their policies.

Titus Edelhofer
Titus Edelhofer 2012년 11월 16일
Hi,
the function you are looking for is urlread
doc urlread
Titus
  댓글 수: 5
Ali Raza Barket
Ali Raza Barket 2021년 3월 30일
Hi,
I also need to download some compressed files from a website. I also tried this method but I got a corrupted file (web content) instead of the desired file.
Here, I solved and it works for me. You need to give the exact URL of the file in the command not just the URL of the page. You can try this code.
url = 'https://github.com/FreeFem/FreeFem-sources/releases/download/v4.8/FreeFEM-4.8-win7-64.exe';
urlwrite (url, 'FreeFEM-4.8-win7-64.exe');
IFM
IFM 2023년 4월 18일
In case anyone still needs to do this, websave is now the best function.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2012년 11월 20일
I've made a batch file in MATLAB with the fprintf() function that is an ftp script (made up of just standard run-of-the-mill ftp commands), then use the system() function to run the ftp script.

Adrian
Adrian 2023년 8월 28일
Yes, MATLAB has the capability to download files from the web. One of the most commonly used functions to download data from the web is webread for retrieving web content and websave for downloading files. Here's how you can use these functions:1. Using websave:
The websave function is useful if you know the exact URL of the file you want to download.
matlabCopy code
% URL of the file to be downloaded url = 'https://example.com/path/to/your/file.txt'; % Specify the local path where you want to save the downloaded file localPath = 'C:\path\to\save\file.txt'; % Use websave to download the file filepath = websave(localPath, url); % Display the path to the saved file disp(['File saved to: ', filepath]);
2. Using webread:
If you're retrieving content rather than a file, you might use webread:
matlabCopy code
url = 'https://api.example.com/data'; data = webread(url); disp(data);
Important Considerations:
  1. Permissions: Ensure you have the necessary permissions to download the content from the website. Some websites may block automated requests or have terms of service that restrict scraping or downloading content.
  2. Web Options: Both webread and websave allow for additional web options to be set, such as setting request timeouts, user agents, or custom headers. You can create these options using weboptions.matlabCopy codeoptions = weboptions('Timeout', 60); % Set a timeout of 60 seconds data = webread(url, options);
  3. Errors & Exceptions: It's good practice to handle potential errors using try-catch blocks. Websites can change, move, or remove content, and your MATLAB code should be robust enough to handle these scenarios without crashing.
Remember to refer to MATLAB's official documentation for any updates or specific nuances about these functions.
  댓글 수: 1
Marco Giangolini
Marco Giangolini 2023년 11월 3일
hi @Adrian, I've the same problem but using both the functions I do not download the file.
With webread I save the HTML webpage content to a local variable, with websave I save the webpage as an HTML file.
How do I navigate among webpages, inserting username and password (if requested), searching for a file in the url specified, and downloading a file resident on that url (pdf, txt, xls, ...)?
Previously I used to create an ftp object and downloading the file with mget, but this doesn't work anymore.
Any suggestion would be really helpful.
Regards,
Marco

댓글을 달려면 로그인하십시오.

카테고리

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