Back to contents PHP Python Ruby Choose a language:

Learn to make a view by copying and pasting code, for programmers or non-programmers (30 minutes).

1. Make a new view

If you haven't already, please do the First scraper tutorial first.

Find the school life expectancy scraper that you made (or use this one we made earlier). Half way down, in the views section, choose "create a new view" and pick Ruby as your language.

Put in a few lines of code, and click the "Preview" button or type Ctrl+P.

puts "This is a <em>fragment</em> of HTML."

A new browser tab will open, and after a moment the rendered output will appear.

2. Query the scraper's datastore

Before you can read the scraper's datastore from the view, you need to attach to it, using its shortname (the name in its URL).

ScraperWiki::attach("school_life_expectancy_in_years")

You can attach to as many datastores as you like. Then you can access their tables directly from queries.

data = ScraperWiki::select( "* from school_life_expectancy_in_years.swdata order by years_in_school desc limit 10" ) puts data

The data comes back as an array of hashes.

3. Print out the results

To output, you simply print to standard output. puts "<table>" puts "<tr><th>Country</th><th>Years in school</th>" for d in data puts "<tr>" puts "<td>", d["country"], "</td>" puts "<td>", d["years_in_school"].to_s, "</td>" puts "</tr>" end puts "</table>"

That's a simple example outputting HTML.

You could output a KML file, an iCal file, an RSS feed, or whatever you need.

What next?

If you have a scraper you want to write, and feel ready, then get going. Otherwise take a look at the documentation.