Using nokogiri with pry

I wanted a quick way to run some XPath selectors against a web page today. Nokogiri comes with a command line tool that you can pass a url and it will drop you into an IRB session. This allows you to play around with some Ruby code to explore a webpage before scraping it.

nokogiri http://example.com

This is useful, but I wanted to use it with Pry. It turns out that adding support for Pry is relatively easy, but I couldn’t find any clear top to bottom instructions, so I’ve documented the process below.

First install Nokogiri and Pry:

gem install nokogiri pry

Then add the following code to ~/.nokogirirc:

require 'pry'
Nokogiri::CLI.console = Pry

That’s it! Now when you use the nokogiri command line tool it will now drop you into a pry REPL. This is perfect for testing your CSS and XPath selectors when you’re writing a scraper.