37
$\begingroup$

A quick question, but which I don't believe has been asked here or at SO.

Does Mathematica have a simple way to just download a file from the web? i.e. if I have a list of PDF links (~ 2,000), can I use Mathematica to quickly take them and save them to my system. The obvious way that I've used in the past is to Import[] the data, but since we're talking large-ish PDFs I wonder if there's a way to skip that step.

I have used wget for this sort of thing in the past, but just seeing if there's an easy way to do it within Mathematica. The 'Web Operations' section of the documentation does not seem to have any obvious reference to this. If not, I will obviously just use the 'proper' tool.

$\endgroup$
32
$\begingroup$

How about a version of:

Needs["Utilities`URLTools`"];
path = FetchURL[
   "http://www-roc.inria.fr/gamma/download/counter.php?dir=MECHANICAL//&\
get_obj=ifp2_cut.mesh.gz&acces=ifp2_cut", "ifp2_cut.mesh.gz"];
$\endgroup$
  • 1
    $\begingroup$ This works great. I've never used this Package, so will read up on it. $\endgroup$ – programming_historian Feb 2 '12 at 18:03
28
$\begingroup$

As a quick update to this, Mathematica 9 added some additional functionality that I have been putting to use - it seems to work very well if you do text analysis, which I do.

Documentation for the new commands URLSave, URLFetch, and the really useful URLSaveAsynchronous are here. For example, I've been running commands like this:

URLSave["http://activehistory.ca","/users/MYPATH/activehistory.txt"];

Which seem to work perfectly.

$\endgroup$
  • 4
    $\begingroup$ This is the one I would recommend with V9 and beyond. The FetchURL function above now internally uses URLFetch and similar functions from the new HTTPClient` package. It supports proxies and lots of options. $\endgroup$ – Joel Klein Jan 16 '13 at 16:49
  • $\begingroup$ for MMA v11+ see navigaid answer below $\endgroup$ – glS Mar 5 '18 at 17:43
13
$\begingroup$

As an update to this question, Mathematica 11.0 introduces a new function URLDownload.

To download a single file, use URLDownload[url,filename]
To download multiple files, try Thread[URLDownload[{url1,url2,…},{filename1,filename2,…}]

Note that the file name part is optional: URLDownload[url] and URLDownload[{url1,url2,…}] are okay. In that case, your file(s) will be randomly named and saved by default to your system temp folder, like "/tmp/64b4ece0-4e3e-417e-9077-567a26c2c9d1.tmp".

$\endgroup$
10
$\begingroup$

You can run wget from within Mathematica. For example if you have your URLs in a text file then just run this in a Mathematica input cell (where on my Mac wget is located in sw/bin):

Run["/sw/bin/wget --no-check-certificate --user-agent=" " -x -i /Users/mypath/urls.txt"]

This is actual code -- with the path changed -- that I run to download information from the Australian stock exchange so I can verify that wget can be run from within Mathematica this way. If you are already set up for using wget then as you probably know it offers advantages over built in Mathematica functions in so far as you can download from sites that require login and do many other useful things.

$\endgroup$
  • $\begingroup$ Fantastic! I've used wget before,esp. when I need to use cookies and such, so this is a great addition to my toolkit. Thanks for this. $\endgroup$ – programming_historian Feb 2 '12 at 22:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.