Tuesday, March 16, 2010

Hpricot? Here we go again

Steven suggested that the list could benefit by showing more human words, aka the site's title. After cursory attempts to find a quick fix, I remembered Hpricot! What the hell did I do for half my thesis if not read data from websites? Some quick memory refreshing was all I needed:

Install:
sudo gem install hpricot

To use:
require 'rubygems'
require 'hpricot'
require 'open-uri'

doc = Hpricot(open(url, 'User-Agent' => 'whatever'))
doc.search("title").text

I guess User-Agent tells the site what browser I'm using? digg wouldn't work without it. Odd...
http://stackoverflow.com/questions/1386985/timeout-error-with-hpricot-in-rails-controller
"title" looks for the title tag hopefully. Seems to work for now.

Added alternating row colors.
http://blogs.csuchico.edu/ik/2006/04/12/alternating-row-colors-with-ruby-on-rails/
http://paulsturgess.co.uk/articles/show/15-alternate-row-classes-with-ruby-on-rails

No comments:

Post a Comment