Pokemon Scraper Lab
Objectives
Set up an
SQLite
databaseScrape and save data into your database
Use data to make ruby objects
Overview
In this lab, you will:
Set up your schema
Scrape data
Insert it into your database
Build out methods to manipulate your data.
This mirrors a very "real world" task.
Set up a local database schema
Find something on the internet that you want to capture to your local database (copyright etc. permitting) and "scrape it"
For each interesting thing found on the internet...
Insert its information into your local database's schema by writing methods
...and then write additional methods to your classes so that you can easily work with your local database
Schema Provided
We have provided a db/schema_migration.sql
file that will run the SQL
statements to set up your database in db/pokemon.db
. You can initialize the database file with touch db/pokemon.db
.
Alert: Make sure that your database is located in the
db
directory!
We can tell sqlite3
to execute a migration script upon a new database with a command like: sqlite3 path/to/database.db < source_of_sql_data
.
Scraper and Scrape-Content Provided
In this example, we're going to take information from a web page about those adorable Pokémon. Instead of having you write a network code that retrieves a Pokémon catalog and scrapes it, we're going to provide you a "scraper" class in lib/scraper.rb
and it's going to "scrape" a local HTML file (pokemon_index.html
). It shouldn't be too hard to see that with just a little bit of extra code we could scrape a "live" web page.
Your scraper should not do the work of inserting rows into the database. Its role is to process the file, it should "hand-off" the creation responsibilities to the Pokemon class. It's appropriate for the scraper to say Pokemon.create...
but it's not appropriate for the scraper to open up a connection to the database.
The goal of an ORM class is to "glue" the scraper to the database.
Speaking of ORM classes...
The Pokemon Class
The Pokemon
class (lib/pokemon.rb
) is responsible for saving, adding, removing, or changing anything about each Pokémon.
It should be handed "raw" Pokémon data from the scraper and handle the ORM work to put the data into the database. It's probably most appropriate to
Create any methods you think you need to make working with the Pokémon data easier.
A Note On Inserting Into the Database
Last updated