Skip to content

Instantly share code, notes, and snippets.

@inopinatus
Forked from sandys/table_to_csv.rb
Last active June 28, 2019 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inopinatus/ef89a7925f52bc8eb14cfeea3a6e1e3a to your computer and use it in GitHub Desktop.
Save inopinatus/ef89a7925f52bc8eb14cfeea3a6e1e3a to your computer and use it in GitHub Desktop.
convert a html table to CSV using ruby
require 'rubygems'
require 'nokogiri'
require 'csv'
f = File.open("table.html")
doc = Nokogiri::HTML(f)
CSV.open("output.csv", 'w') do |csv|
doc.xpath('//table/tbody/tr').each do |row|
tarray = []
row.xpath('td').each do |cell|
tarray << cell.text.gsub(/\n/, ' -- ').gsub(/\s+/, ' ').strip
end
csv << tarray
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment