Scraping Recipes with Nokogiri
Tags: ruby, rails, javascript, scraping
Published: 2019-05-15 19:34:58

---
title: Scraping Recipes with Nokogiri
published: true
description: An introduction to scraping with Nokogiri.
tags: Ruby, Rails, JS, scraping
cover_image: https://proxy.duckduckgo.com/iu/?u=http%3A%2F%2F2.bp.blogspot.com%2F-R4ZtwSbrFuE%2FUDEPFN_ZyQI%2FAAAAAAAAB3c%2FoPxZSB6RueM%2Fs1600%2FWood-work-cake2.jpg&f=1
---

I'm working on a project that requires scraping recipes from the front end of a variety of sites.  An incredible journey!  And the friend I made along the way was right beside me all along!  
##**NOKOGIRI.**
Most of you probably know Nokogiri as the point in your Ruby on Rails install where you take a break to get a beverage.  (Why is Nokogiri bundled with every flavor of Rails on Earth?  I have no idea.*  It's quite large, and specialized.)  But here you are, already on a first-name basis with a world-class web scraper.  Want to keep tabs on something that doesn't have an RSS feed?  Aggregate content from a whole category of sites?  Amass training data for your latest NLP project?  You've come to the right place.

Nokogiri comes with tools for scraping three main formats: CSS, HTML, and XML.  I used a combination of the HTML and CSS tools.  If you like Javascript, a Nokogiri object gives you the equivalent of `document.querySelectorAll` on documents not your own.  Then you get to add as much Ruby sugar on top as you like.  If that doesn't sound delicious, you are reading the wrong blog post.

|<img src="http://www.peppercreekfarms.com/images/Ruby%20Shimmer%20Sugar.jpg" width="250" />|
|:--------:|
| *fig 1. Ruby sugar, anyone?* |

The first thing you'll need is to request a copy of the page.  I used a gem called HTTParty, for which I took the onerous step of adding `'gem httparty'` to my gemfile.  Requesting with HTTParty looks like this:

![](https://thepracticaldev.s3.amazonaws.com/i/fr8abcco28q0rpft6gu1.png)

Tricky.  Bear with me here: loading this into a Noko object is kinda gnarly.

![](https://thepracticaldev.s3.amazonaws.com/i/l47rsnn52h56073inu67.png)

Oh wait, that was incredibly easy.  Just to be clear, we told NG to parse the HTTParty output as HTML, and store it in an instance variable.  Why an instance variable?  Let's take a moment to reflect on our project setup.

I recommend separating the scraper object from the model (or models) you use to store the results.  In the scraper, you can keep: 

* scraping methods
* the URL of interest
* your Nokogiri page object
* your scraped "seed data"

Then you can use the seed data to generate the result objects in your database.  Keeping these separate means you're not wasting a bunch of database columns in either model, or giving your results methods they'll never use.

In my scraper DB objects, I kept the Noko page, recipe title, URL, yield/cook time, and arrays of strings for ingredients and instructions.  Since I'm on Postgres, setting up the array attributes was a piece of cake: 

> `$ rails g migration add_columns_to_scrapers`

and in your new migration:

```ruby
add_column :scrapers, :ingredients, :string, array: true, default: []
add_column :scrapers, :instructions, :string, array: true, default: []
```
|![piece of cake](https://proxy.duckduckgo.com/iu/?u=http%3A%2F%2Ffootage.framepool.com%2Fshotimg%2F113591971-piece-of-cake-cake-plate-cake-fork-strawberry-mousse.jpg&f=1)|
|:--------------:|
| *fig 2. Cake.* |

If you're on another database, you might create join tables for these attributes, which is slightly less fun.  But hey, you're not using Postgres, so . . tough nuts?  Please be advised, I won't be sparing on the food puns.

Anyway.  Now the fun part: the scraping methods!

I was worried I'd have to construct monster conditional regex statements to parse ingredients lists.  Fortunately I was working with well-designed commercial apps, with nice unique classnames.  So, I used Nokogiri's CSS method to scrape my data instead.  

At the point after you capture your page, set a debugger so you can play with the results.  OK, let's see what the whole thing looks like.

>`@page`

|![](https://thepracticaldev.s3.amazonaws.com/i/v8fun6yn2gszby6sz5lg.png)|
|:--:|
| *fig 3. Send help pls.* |

Oof.  This is the first "hard-looking" thing we've come across.  It's really not so bad.  What you're seeing is the Nokogiri object version of every single element and sub-element in the page.  This is actually pretty awesome - it means you have very fine control over the results.

The recipe title was my first target.  So, I opened up Chrome's Inspect Element, and got the class of the `<h1>` title.  

![](https://thepracticaldev.s3.amazonaws.com/i/x9rz5o5401buenynvp7j.png)

There it is! The conveniently named `page-title`. Now we can use the `.css` method:

> `@page.css(".page-title")`

Phew.  Still with me?  We added the `.` to `.page-title` to tell Nokogiri we want to search for that term as a class name.  Well, it turns out Noko gives us a little more than we want.  Here are the results of that query:

![](https://thepracticaldev.s3.amazonaws.com/i/ypoat9f5ote74gmc154y.png)

Fortunately, we can use the `.text` method on any of these to, well, get the text. This is a lot like using `.textContent` in JS.

> `@page.css(".page-title").text`

![](https://thepracticaldev.s3.amazonaws.com/i/sio9buimxtv0vrf95bex.png)

We're getting close!  All we need is Ruby's excellent `.strip` method, which gets rid of whitespace and newlines.

> `@page.css(".page-title").text.strip`

![](https://thepracticaldev.s3.amazonaws.com/i/l3rnpiplhbrazqlw204w.png)

Ta-da!  You've just scraper-Hello-Worlded.

For all you Ruby chainsmokers like me, getting the array of ingredients is a treat.

![](https://thepracticaldev.s3.amazonaws.com/i/n070dspy705dggo6kv4e.png)

This is much the same as the above. 

1. `css("li .component-name")` gets back an array of Noko objects, 
1. which we convert to a long string with `.text`.  
1. Then we `split` on newlines,
1. and map `strip` onto each item from the split array. (If that syntax looks weird to you, you can write it as `.map {|i| i.strip }`.) 
1. That gives us a few empty strings as well, so we select the non-empties.  

Result:

![](https://thepracticaldev.s3.amazonaws.com/i/47ivi7zbh297x9nvv86j.png)

Win.  Now I had to parse the ingredients strings for amount, measurement, and name.  That is another blog post all its own, though.  For now, enjoy your newfound skillz!

|![](http://dailypicksandflicks.com/wp-content/uploads/2012/04/mother-of-god-you-graduated-cake.jpg)|
|:---:|
|*fig 4. Cake.*|

Thanks for reading.  Let me know if anything needs to be cleared up or expanded on.

**OK, I googled it.  Rails includes Nokogiri because it's a dependency of the test suite `rails-dom-testing`, which makes sense I guess, since you're going to want to parse your page for testing.  Grumble grumble.*