从网站下载数据 [英] Downloading data from website

查看:91
本文介绍了从网站下载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码从网站下载文件夹中的两个文件.

I use the following code for downloading two files in a folder from a website.

我要下载页面中包含"MOD09GA.A2008077.h22v05.005.2008080122814.hdf"和"MOD09GA.A2008077.h23v05.005.2008080122921.hdf"的某些文件.但是我不知道如何选择这些文件.下面的代码下载了所有文件,但我只需要其中两个.

I want to download some files that contain "MOD09GA.A2008077.h22v05.005.2008080122814.hdf" and "MOD09GA.A2008077.h23v05.005.2008080122921.hdf" in the page. But I don't know how to select these files. The code below download all the files, but I only need two of them.

有人有什么想法吗?

URL = 'http://e4ftl01.cr.usgs.gov/MOLT/MOD09GA.005/2008.03.17/';
% Local path on your machine
localPath = 'E:/myfolder/';

% Read html contents and parse file names with ending *.hdf
urlContents = urlread(URL);
ret = regexp(urlContents, '"\S+.hdf.xml"', 'match');


% Loop over all files and download them
for k=1:length(ret)
    filename = ret{k}(2:end-1);
    filepathOnline = strcat(URL, filename);
    filepathLocal = fullfile(localPath, filename);
    urlwrite(filepathOnline, filepathLocal);
end

推荐答案

尝试使用tokens的正则表达式:

Try the regexp with tokens instead:

localPath = 'E:/myfolder/';
urlContents = 'aaaa "MOD09GA.A2008077.h22v05.005.2008080122814.hdf.xml" and "MOD09GA.A2008077.h23v05.005.2008080122921.hdf.xml"  aaaaa';

ret = regexp(urlContents , '"(\S+)(?:\.\d+){2}(\.hdf\.xml)"', 'tokens');
%// Loop over each file name
for k=1:length(ret)
    filename = [ret{k}{:}];
    filepathLocal = fullfile(localPath, filename)
end

这篇关于从网站下载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆