Download file using "websave" from a website that requires login
조회 수: 4 (최근 30일)
이전 댓글 표시
I am trying to download the following file from NASA's Archive of Space Geodesy Data:
but I am having a hard time downloading the file to my computer using websave because downloading the file requires login information.
When the file download link is clicked, it first redirects to a login page, and when the correct credentials are entered, it then redirects back to the original link (which is the one above) and the file is downloaded. I was wondering whether there is a way to download the file despite the login information and redirection being required. Currently, my code returns an html file of the login page.
Here is the code I am using if it is of any help:
filename = 'RINEXFile.rnx.gz';
fileurl = 'https://cddis.nasa.gov/archive/gnss/data/daily/2021/143/21h/MAR700SWE_R_20211430000_01D_SN.rnx.gz'
websave(filename,fileurl)
댓글 수: 5
Irem Köz
2022년 5월 2일
I wonder whether there is any progress about this problem or not? I also have the same problem with the same website.
답변 (1개)
Abhiram
2025년 2월 17일
편집: Abhiram
2025년 2월 17일
In order to to download the file, you can follow the given steps:
- The login information must be specified using a 'POST' request to the login form.
- The web cookies will be provided in the response message.
- These web cookies should then be used in a 'GET' request to download the necessary file.
The code given below shows an example on how to implement 'POST' and 'GET' requests:
import matlab.net.*;
import matlab.net.http.*;
import matlab.net.http.field.*;
% POST Request
uriPost = URI('https://www.httpbin.org/post');
data = struct ('msg', 'hello world', 'date', datetime('now'));
acceptHeader = AcceptField('application/json');
contentType = ContentTypeField('application/json');
headers = [contentType, acceptHeader];
httpOpts = HTTPOptions;
r = RequestMessage ('POST', headers, data);
[resp, req, hist] = r.send(uriPost, httpOpts);
out = resp.Body.Data;
% GET Request
uriGet = URI('https://www.httpbin.org/get');
acceptHeader = AcceptField('application/json');
headers = acceptHeader;
httpOpts = HTTPOptions;
r = RequestMessage('GET',headers);
[resp, req, hist] = r.send(uriGet, httpOpts);
out = resp.Body.Data;
For additional guidance, you can refer to the MATLAB documentation for “RequestMessage”, “Cookie”, and “FormProvider” classes:
댓글 수: 0
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!