Everything we’ve done so far in this book has been done entirely within the Wolfram Language and the Wolfram Knowledgebase. But sometimes you need to get things from the outside. Needless to say, they often won’t be as clean and organized as what we’re used to inside the Wolfram Language—and they may change without warning.

As a first example, let’s import the text from the front page of the United Nations website. We can do this using the function Import.

Import["http://un.org"]

The result is a string, possibly with some blank lines. Let’s start by splitting the string at newlines.

StringSplit[Import["http://un.org"],"\n"]

LanguageIdentify[StringSplit[Import["http://un.org"],"\n"]]

Import lets you import a wide variety of different elements. "Hyperlinks" gets hyperlinks that appear on a webpage; "Images" gets images.

Import["http://un.org","Hyperlinks"]

Import["http://wikipedia.org","Images"]

As a more sophisticated example, here’s a graph of the hyperlinks in part of my website. To keep it manageable, I’ve taken just the first 5 hyperlinks at each level, and gone only 3 levels.

NestGraph[Take[Import[#,"Hyperlinks"],5]&,"http://stephenwolfram.com",3]

The Wolfram Language can import hundreds of formats—including spreadsheets, images, sounds, geometry, databases, log files and more. Import will automatically look at the file extension (.png, .xls, etc.) to determine what to do.

Import["http://www.stephenwolfram.com/img/homepage/stephen-wolfram-portrait.png"]

Classify["NotablePerson",%]

As well as importing from the web, Import can also import from your own files, stored in your computer system or in the Wolfram Cloud.

The Wolfram Language lets you not only deal with webpages and files, but also with services or APIs. For example, SocialMediaData lets you get data from social media services—at least once you’ve authorized them to send the data.

SocialMediaData["Facebook","FriendNetwork"]

Another external service the Wolfram Language can access is web search.

WebImageSearch["colorful birds"]

WebImageSearch["colorful birds","Thumbnails"]

ImageIdentify/@%

An important source of external data for the Wolfram Language is the Wolfram Data Repository. The data in this repository comes from many places—but it’s all been set up to be easy to work with in the Wolfram Language.

You can find out what’s available by browsing the Wolfram Data Repository.

Once you've found something you want, just use ResourceData["name"] to get it into the Wolfram Language.

WordCloud[ResourceData["On the Origin of Species"]]

In addition to getting things into the Wolfram Language, you can also send things out. For example, SendMail sends email from the Wolfram Language.

SendMail["Hello from the Wolfram Language!"]

SendMail["test@wolfram.com",{"Wolf","Here's a wolf...", }]

If you want to interact with external programs and services, you’ll often have to export things from the Wolfram Language.

CloudExport[Graphics[Circle[ ]],"PDF"]

You can also export to local files using Export.

Export["primepowers.xls",Table[Prime[m]^n,{m,10},{n,4}]]

Here’s part of the resulting file:

Import["primepowers.xls"]

The Wolfram Language can import and export hundreds of formats, of many different kinds.

Export["spikey.stl",LinguisticAssistant["Image"]]

Here’s the result of 3D printing from the spikey.stl file:

Creating 3D geometry in a form suitable for printing can be quite complicated. The function Printout3D does all the steps automatically—and it can also send the final geometry to a 3D printing service (or to your own 3D printer, if you have one).

Graphics3D[Sphere[RandomReal[5,{30,3}]]]

Printout3D[%,"Sculpteo"]

Import the images from http://google.com. »

Make an image collage of disks with the dominant colors from images on http://google.com. »

Make a word cloud of the text on http://bbc.co.uk. »

Make an image collage of the images on http://nps.gov. »

Use ImageInstanceQ to find pictures on https://en.wikipedia.org/wiki/Ostrich that are of birds. »

Use TextCases with "Country" to find instances of country names on http://nato.int, and make a word cloud of them. »

Find the number of links on https://en.wikipedia.org. »

Send yourself mail with a map of your current location. »

Send yourself mail with an icon of the current phase of the moon. »

Why do I get different results when I run the website examples?

Because the websites have changed!  You’ll get whatever they have on them now.

Why doesn’t Import retrieve elements that I see when I visit a webpage in a browser?

Probably because they’re not directly present in the HTML source of the webpage, which is what Import looks at. They’re probably being dynamically added using JavaScript.

Can I import a local file from my computer if I’m using the cloud?

Yes. Use the upload  button in the cloud system to upload the file into your cloud file system, then
use Import.

What formats can Import handle?

See the list at wolfr.am/ref-importexport or evaluate $ImportFormats.

How do Import and Export figure out what format to use?

You can explicitly tell them. Or they can determine it from file extensions, like .gif or .mbox.

Where does Export put the files it creates?

If you don’t give a directory in the file name you specify, it’ll go in your current directory. You can open it as your operating system would using SystemOpen, and you can delete it with DeleteFile.

What is an API?

An application program interface. It’s an interface that a program exposes to other programs, rather than to humans. The Wolfram Language has several APIs, and lets you create your own using APIFunction.

How do I authorize a connection to an external account of mine?

When you use SocialMediaData or ServiceConnect, you’ll typically be prompted to authorize the Wolfram Connection app for that particular service.

Tech Notes