Main Content

urlwrite

Download URL content and save to file (not recommended)

urlwrite is not recommended. For http or https protocols, use webread or webwrite instead. For ftp protocols, use ftp functions. For file protocols, use fileread, fopen or copyfile.

Description

urlwrite(URL,filename) reads web content at the specified URL and saves it to the file specified by filename.

example

urlwrite(URL,filename,Name,Value)uses additional options specified by one or more Name,Value pair arguments.

example

[filestr,status] = urlwrite(___) stores the file path in variable filestr, and suppresses the display of error messages, using any of the input arguments in the previous syntaxes. When the operation is successful, status is 1. Otherwise, status is 0.

Examples

collapse all

Download the HTML for the page on the MATLAB® Central File Exchange that lists submissions related to urlwrite. Save the results to samples.html in the current folder.

fullURL = ['https://www.mathworks.com/matlabcentral/fileexchange' ...
           '?term=urlwrite'];
filename = 'samples.html';
urlwrite(fullURL,filename);

View the file.

web(filename)

Download the HTML for the page on the MATLAB Central File Exchange that lists submissions related to urlwrite. Save the results to samples.html in the current folder.

URL = 'https://www.mathworks.com/matlabcentral/fileexchange';
filename = 'samples.html';
urlwrite(URL,filename,'get',{'term','urlwrite'});

urlwrite downloads the HTML content from https://www.mathworks.com/matlabcentral/fileexchange/?term=urlwrite and writes it to samples.html.

Download content from a page on the MATLAB Central File Exchange as in the first example, and specify a timeout duration of 5 seconds.

fullURL = ['https://www.mathworks.com/matlabcentral/fileexchange' ...
	   '?term=urlwrite'];
filename = 'samples.html';
urlwrite(fullURL,filename,'Timeout',5);

Input Arguments

collapse all

Content location, specified as a character vector. Include the transfer protocol, such as http, ftp, or file.

Example: 'https://www.mathworks.com/matlabcentral'

Name of the file to store the web content, specified as a character vector. If you do not specify the path for filename, urlwrite saves the file in the current folder.

Example: 'myfile.html'

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: 'Timeout',10,'Charset','UTF-8' specifies that urlwrite should time out after 10 seconds, and the character encoding of the file is UTF-8.

Parameters of the data to send to the web form using the GET method, specified as the comma-separated pair consisting of 'get' and a cell array of paired parameter names and values. The supported parameters depend upon the URL.

'Get' includes the data in the URL, separated by ? and & characters.

Example: 'Get',{'term','urlread'}

Parameters of the data to send to the web form using the POST method, specified as the comma-separated pair consisting of 'post' and a cell array of paired parameter names and values. The supported parameters depend upon the URL.

'Post' submits the data as part of the request headers, not explicitly in the URL.

Character encoding, specified as the comma-separated pair consisting of 'Charset' and a character vector. If you do not specify Charset, the function attempts to determine the character encoding from the headers of the file. If the character encoding cannot be determined, Charset defaults to the native encoding for the file protocol, and UTF-8 for all other protocols.

Example: 'Charset','ISO-8859-1'

Timeout duration in seconds, specified as the comma-separated pair consisting of 'Timeout' and a scalar. The timeout duration determines when the function errors rather than continues to wait for the server to respond or send data.

Example: 'Timeout',10

Client user agent identification, specified as the comma-separated pair consisting of 'UserAgent' and a character vector.

Example: 'UserAgent','MATLAB R2012b'

HTTP authentication mechanism, specified as the comma-separated pair consisting of 'Authentication' and a character vector. Currently, only the value 'Basic' is supported. 'Authentication','Basic' specifies basic authentication.

If you include the Authentication argument, you must also include the Username and Password arguments.

User identifier, specified as the comma-separated pair consisting of 'Username' and a character vector. If you include the Username argument, you must also include the Password and Authentication arguments.

Example: 'Username','myName'

User authentication password, specified as the comma-separated pair consisting of 'Password' and a character vector. If you include the Password argument, you must also include the Username and Authentication arguments.

Example: 'Password','myPassword123'

Output Arguments

collapse all

Path of the file specified by filename, returned as a character vector.

Download status, returned as either 1 or 0. When the download is successful, status is 1. Otherwise, status is 0.

Tips

  • urlread and urlwrite can download content from FTP sites. Alternatively, use the ftp function to connect to an FTP server and the mget function to download a file.

Version History

Introduced before R2006a