Saturday, 26 December 2009

A Ruby script to search bookstores online

I started dabbling in Ruby some weeks back. The initial interest was sparked after reading "Treating Code as an Essay" (Yukihiro Matsumoto) - one of the chapters in Beautiful Code. So I started doing these bootstrapping exercises in Ruby. Some of the exercises are good - but nothing beats doing a small project to learn a new language.

I buy a lot of books, mostly online. There are a few good online bookstores in India, notably, and (Sadly, Amazon does not have full-fledged shipping to India yet). The way I usually search for a book in online bookstores is (was, till now)

  1. Go to and enter the book title
  2. Click on the best match
  3. Click on 'All Sellers' on the left of the page
  4. The Indian bookstores are usually listed towards the bottom. It does not include all stores, and sometimes the prices are not listed. I have to go to each individual site and check them out.

I wanted to collapse these steps into one - a simple script that would accept the name of the book and show results from all these bookstores, with comparative pricing. And the result was this

It's in Ruby, runs from the command line and writes the output to an HTML in the same directory called 'search.html'. Much needs to be done, like

  • Price based listing with the lowest on top
  • A web interface for the search
  • Add more bookstores - it's only,, Indiaplaza and right now.

To run the script, type this (you need Ruby 1.8.x, available from and the Hpricot HTML parser library, available from
ruby lib\book-search.rb "<book title (in quotes if it has spaces)>"


  1. Hrish,
    Amazon now ships CDs and Books to India. but their shipping charges are very high.

  2. @Surya: Yes. Also, a lot of the Indian editions of technical books that you can buy here cost much less than the original editions :).
    I also found this - - where you can search for items that ship internationally.

  3. [...] made occasional forays into Python and Ruby for some side projects and [...]

  4. Hey Hrish, this is very interesting..

    I have been thinking of having something which would search all online stores for a particular item product name or code and bring to me the price, any offers on that product and other details like shipping rates, time etc.. Is this possible?
    I need to learn ruby or anything which will help me achieve this...

  5. @Shri: The script I wrote visits each website and "scrapes" (i.e., fetches and parses) the HTML and shows the results. So what you mention is definitely possible. You'll have to analyze the structure of the search results page of each online store you wish to include in your results.