html2csv

Does what it says on the tin. Do “gem install nokogiri” or “sudo apt-get install libnokogiri-ruby” if necessary.

#!/usr/bin/env ruby

require 'rubygems'
require 'nokogiri'
require 'csv'

def main(f)
  Nokogiri::HTML.parse(open(f)).search('table').each do |t|
    t.search('tr').each do |tr|
      puts CSV.generate_line(tr.xpath('th|td').collect do |td|
        td.text
      end)
    end
    puts
  end
end

ARGV.each { |f| main(f) }
This entry was posted in Code. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>