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 Python as your language.

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

print "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).

import scraperwiki scraperwiki.sqlite.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.sqlite.select( '''* from school_life_expectancy_in_years.swdata order by years_in_school desc limit 10''' ) print data

The data comes back as an array of dictionaries.

3. Print out the results

To output, you simply print to standard output. print "<table>" print "<tr><th>Country</th><th>Years in school</th>" for d in data: print "<tr>" print "<td>", d["country"], "</td>" print "<td>", d["years_in_school"], "</td>" print "</tr>" print "</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.