public
Description: A cute HTML scraper / data extraction tool
Homepage:
Clone URL: git://github.com/mislav/scraper.git
name age message
file LICENSE Fri Nov 06 11:07:45 -0800 2009 MIT licensed closes #3 [mislav]
file README.md Sat Oct 24 06:56:03 -0700 2009 add delicious.com, Twitter JSON sample scripts [mislav]
file Rakefile Sat Oct 24 06:56:03 -0700 2009 add delicious.com, Twitter JSON sample scripts [mislav]
directory examples/ Sun Oct 25 05:25:33 -0700 2009 we didn't need that extra `convert_document` in... [mislav]
file scraper.rb Sun Oct 25 05:40:44 -0700 2009 declare methods that usually shouldn't be overr... [mislav]
README.md

Scraper

Scraper is a cute HTML screen-scraping tool.

require 'scraper'
require 'open-uri'

class BlogScraper < Scraper
  element :title

  elements 'div.hentry' => :articles do
    element 'h2' => :title
    element 'a/@href' => :url
  end
end

blog = BlogScraper.parse open('http://example.com')

blog.title
#=> "My blog title"

blog.articles.first.title
#=> "First article title"

blog.articles.first.url
#=> "http://example.com/article"

There are sample scripts in the "examples/" directory; run them with:

ruby -rubygems examples/<script>.rb

See the wiki for more on how to use Scraper.

Requirements

None. Well, Nokogiri is a requirement if you pass in HTML content that needs to be parsed, like in the example above. Otherwise you can initialize the scaper with an Hpricot document or anything else that implements at(selector) and search(selector) methods.