How would I get a list of filenames available at a URL? For example, list the files at:
http://www1.ncdc.noaa.gov/pub/data/cmb/drought/weekly-palmers/2005/
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityHow would I get a list of filenames available at a URL? For example, list the files at:
http://www1.ncdc.noaa.gov/pub/data/cmb/drought/weekly-palmers/2005/
Does this do the job?
links = Import[
"http://www1.ncdc.noaa.gov/pub/data/cmb/drought/weekly-palmers/2005/",
"Hyperlinks"]
{"http://www1.ncdc.noaa.gov/pub/data/cmb/drought/weekly-palmers/2005/?C=N;O=D",
"http://www1.ncdc.noaa.gov/pub/data/cmb/drought/weekly-palmers/2005/?C=M;O=A",
"http://www1.ncdc.noaa.gov/pub/data/cmb/drought/weekly-palmers/2005/?C=S;O=A",
...
then you can cherry-pick the ones you want using, eg,StringCases
:
DeleteCases[StringCases[links, __ ~~ "gif"], {}]
{{"http://www1.ncdc.noaa.gov/pub/data/cmb/drought/weekly-palmers/2005/pdi20050416-pg.gif"},
{"http://www1.ncdc.noaa.gov/pub/data/cmb/drought/weekly-palmers/2005/pdi20050423-pg.gif"},
{"http://www1.ncdc.noaa.gov/pub/data/cmb/drought/weekly-palmers/2005/pdi20050430-pg.gif"},
{"http://www1.ncdc.noaa.gov/pub/data/cmb/drought/weekly-palmers/2005/pdi20050507-pg.gif"}, ...
As is commonly recommended in other Q&As for tasks such as this, we can also use Cases
:
baseurl = "http://www1.ncdc.noaa.gov/pub/data/cmb/drought/weekly-palmers/2005/";
source = Import[baseurl, "XMLObject"];
baseurl <> # & /@ Rest@Cases[source, XMLElement["td", __,
{XMLElement["a", __, {___ ..., c_, ___ ...}]}
] :> c, Infinity]
{"http://www1.ncdc.noaa.gov/pub/data/cmb/drought/weekly-palmers/2005/pdi20050101.wkly", "http://www1.ncdc.noaa.gov/pub/data/cmb/drought/weekly-palmers/2005/pdi20050108.wkly", "http://www1.ncdc.noaa.gov/pub/data/cmb/drought/weekly-palmers/2005/pdi20050115.wkly", "http://www1.ncdc.noaa.gov/pub/data/cmb/drought/weekly-palmers/2005/pdi20050122.wkly",...
Or just remove baseurl <> # & /@
if you don't want the full URLs, but only the file names.
Although I am sure that this can be done in Mathematica, I already know how to do it in R. For example, see the answer at https://stackoverflow.com/questions/12930933/retrieve-the-list-of-files-from-a-url
Thanks to RLink, I can use this solution directly in Mathematica
(*Initialise R*)
Needs["RLink`"]
InstallR[]
(*Install the XML package*)
REvaluate["install.packages('XML')"]
rdata = REvaluate["
{
library(XML)
readHTMLTable(\"http://www1.ncdc.noaa.gov/pub/data/cmb/drought/\
weekly-palmers/2005/\",
skip.rows=1:2)[[1]]$Name -> file.list
}
"];
filenames = DeleteCases[RFactorToVector[rdata], _Missing];
Lets look at the first 10 filenames:
In[98]:= filenames[[1 ;; 10]]
Out[98]= {"pdi20050101.wkly", "pdi20050108.wkly", "pdi20050115.wkly", \
"pdi20050122.wkly", "pdi20050129.wkly", "pdi20050205.wkly", \
"pdi20050212.wkly", "pdi20050219.wkly", "pdi20050222.wkly", \
"pdi20050226.wkly"}
install.packages
part, and they would be right ...
$\endgroup$
Oct 2, 2013 at 10:00
I like this way. It's similar to @Anon but with simpler Cases
syntax:
baseurl="http://www1.ncdc.noaa.gov/pub/data/cmb/drought/weekly-palmers/2005/";
source=Import[baseurl,"XMLObject"];
files=Cases[source,{_,"href"->file_/;StringMatchQ[file,"*.*"]}:> file ,\[Infinity]];
baseurl<>#&/@files
And you could easily choose file format, using for example "*.gif"
instead of "*.*"